首页 > 解决方案 > 一个键在多个相同类型的结构上的映射是否可能?

问题描述

我正在尝试将一个地址映射到属于同一地址的多个相同类型的结构上。如果我想在之后根据请求为一个地址选择任何“存储”结构,我该怎么做?

我创建了一个名为 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);
}

现在我有一个函数,我可以调用一个病人的所有书面处方。实际上,我只能为一个地址调用最后的书面处方。

标签: structmappingblockchainethereumsolidity

解决方案


当然,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);
}

推荐阅读