一个键在多个相同类型结构上的一致性映射是否可能?
Is the mapping in solidity of one key on multiple structs of same type possible?
我正在尝试将一个地址映射到属于同一地址的多个相同类型的结构。如果之后我想根据请求为一个地址选择任何 "stored" 结构,我该怎么做?
我创建了一个名为 Prescription 的结构,以及一个与患者地址的映射。所以我真正想要的是将患者地址映射到几个处方结构。
struct Prescription {
address patients_address;
string medicament;
string dosage_form;
uint amount;
uint date;
}
mapping (address => Prescription) ownerOfPrescription;
address [] public patients;
function createPrescription(address patients_address, string medicament, string dosage_form, uint amount, uint date) public restricted {
var newPrescription = ownerOfPrescription[patients_address];
newPrescription.medicament = medicament;
newPrescription.dosage_form = dosage_form;
newPrescription.amount = amount;
newPrescription.date = date;
patients.push(patients_address) -1;
}
function getPre(address _address)view public returns (string, string, uint, uint){
return(
ownerOfPrescription[_address].medicament,
ownerOfPrescription[_address].dosage_form,
ownerOfPrescription[_address].amount,
ownerOfPrescription[_address].date);
}
现在我会有一个功能,我可以调用一个病人的所有书面处方。其实我只能叫一个地址的最后一张处方。
当然,a mapping
的值类型可以是数组:
// map to an array
mapping (address => Prescription[]) ownerOfPrescription;
function createPrescription(...) ... {
// add to the end of the array
ownerOfPrescription[patients_address].push(Prescription({
medicament: medicament,
...
});
patients.push(patients_address);
}
我正在尝试将一个地址映射到属于同一地址的多个相同类型的结构。如果之后我想根据请求为一个地址选择任何 "stored" 结构,我该怎么做?
我创建了一个名为 Prescription 的结构,以及一个与患者地址的映射。所以我真正想要的是将患者地址映射到几个处方结构。
struct Prescription {
address patients_address;
string medicament;
string dosage_form;
uint amount;
uint date;
}
mapping (address => Prescription) ownerOfPrescription;
address [] public patients;
function createPrescription(address patients_address, string medicament, string dosage_form, uint amount, uint date) public restricted {
var newPrescription = ownerOfPrescription[patients_address];
newPrescription.medicament = medicament;
newPrescription.dosage_form = dosage_form;
newPrescription.amount = amount;
newPrescription.date = date;
patients.push(patients_address) -1;
}
function getPre(address _address)view public returns (string, string, uint, uint){
return(
ownerOfPrescription[_address].medicament,
ownerOfPrescription[_address].dosage_form,
ownerOfPrescription[_address].amount,
ownerOfPrescription[_address].date);
}
现在我会有一个功能,我可以调用一个病人的所有书面处方。其实我只能叫一个地址的最后一张处方。
当然,a mapping
的值类型可以是数组:
// map to an array
mapping (address => Prescription[]) ownerOfPrescription;
function createPrescription(...) ... {
// add to the end of the array
ownerOfPrescription[patients_address].push(Prescription({
medicament: medicament,
...
});
patients.push(patients_address);
}