How to get same entity from metadata with multiple values?

Hello.

I want to use a metadata with multiple values for same entity and same user.

My scenario with actions is:

1. Bob buys Fruit - Apple

create_metadata($entity_guid, 'fruit', 'apple', 'text', elgg_get_logged_in_user_guid(), ACCESS_PUBLIC);

2. Bob buys Fruit - Kiwi

create_metadata($entity_guid, 'fruit', 'kiwi', 'text', elgg_get_logged_in_user_guid(), ACCESS_PUBLIC);

3. Now I want to display all Bob's Fruits:

$options = [
    'types' => 'object',
    'subtypes' => 'subtype',
    'metadata_owner_guid' => elgg_get_logged_in_user_guid(),
    'metadata_name_value_pairs' => [
        'name' => 'fruit',
        'value' => ELGG_ENTITIES_ANY_VALUE,
    ],
    'limit' => 10,
    'pagination' => true,
    'full_view' => false,
];

echo elgg_list_entities_from_metadata($options);

But this scenario returns an entity with last created metadata only, e.g. kiwi in my case.

I want to show apple and kiwi both.

Where I wrong?

Or it's impossible using metadata functions (I tried with annotations also, same errors)?

Thank you.

  • You can only have a single metadata with the same name belowing to one entity. So, the second create_metadata call does not create a new metadata entry but re-uses the existing metadata entry and overwriting the value. It's like doing

    $entity->fruit = "apple";

    $entity->fruit = "kiwi";

    Now "kiwi" has overwritten "apple".

    You could deal with the fruits as array and use implode and explode to convert the array to a string (as you couldn't save an array as metadata value). Or you could handle the fruits with entities (type would be object and subtype would be fruits). Entity owner would indicate who the fruits belong to. It all depends on what you have in mind later on with all the fruits on how to implement it best.

  • fruit as a subtype is not suitable for my case because I've 'subtype => product' already.

    I want to have different types (fruit, vegetable) of this subtype (product) and different types of fruits and vegetables.

    So, user could choose 1 product - 1 fruit - multitypes of fruit.

    (Sorry if I do not clearly explains).

    I will try your first suggestion (fruits as array) and will be apprecate if you can give some example for it.

    Thank you, iionly

  • @maxvin Look at tags lib.

  • It really depends on what you have in mind how to implement it best. Would it make sense to use more than just a single metadata field to get it working? Entity type is object and subtype is product. And then you could have metadata "category" (on possible value would be "food") and the "subcategory" ("fruit", "vegetable" etc) and metadata "product_name" ("apple", "kiwi" etc.). There could be other metadata for products like "amount" and whatever might be useful.

    A second type of entities could be used to handle an "order" made by a user (or whatever makes sense to be used as name for the choice of the users). And the selected items could again be handled for example by metadata referring to the guid of the chosen product. With every product handled with a single entity you have unique guids for every product. Another possibility would be to add a relationship between the order entity and the product entity. In this case you wouldn't have to deal with guids. But the relationship would only sign that a certain product would be part of an order but couldn't deal for example with the amount (how many kiwis?).

  • Indeed, I've forgot about categories ;)

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking