How to get Entity based on combination of metadata

Hello Elgger,

I want to get list of entity based on combination of metadatas. I have used below function and passing proper argument as well but when i see generated query it shows unexpected  where condition.

Function: elgg_get_entities_from_metadata

metadata_names => array('action', 'key')

metadata_values => array('search', '1234567');

Generated Query:

SELECT DISTINCT e . *
FROM elgg_entities e
JOIN elgg_metadata n_table ON e.guid = n_table.entity_guid
JOIN elgg_metastrings msn ON n_table.name_id = msn.id
JOIN elgg_metastrings msv ON n_table.value_id = msv.id
WHERE (
( (msn.string IN ( 'action', 'key') ) AND ( BINARYmsv.string IN ( 'search', '1234567' ))
AND ( ( 1 =1 ) AND n_table.enabled = 'yes' ))
AND ( n_table.owner_guid IN ( 35675 )))
AND (e.site_guid IN ( 1 ))
AND (( 1 =1 )
AND e.enabled = 'yes'
)
ORDER BY e.time_created DESC
LIMIT 0 , 10

Expected where condition:

action='search' AND key='1234567'

Looking forward to have a some suggestion.

Many thanks!

Mit

 

 

  • Hi miteshsc, here how I will do this:

    $options['metadata_name_value_pairs'] = array(
                    array(
                            'name'  => 'action',
                            'value' => 'search'               
                    ),
                    array(
                        'name' => 'key',
                        'value' => '1234567'
                    ),
                );
       
     $entities = elgg_get_entities_from_metadata($options);

     

  • Hey ElggCloud,

    Thanks a lot. Cheers!

  • Now i am tryin to save another entity with different metadatas action=>"save" , key=>"1234567"  and i am passing same key(1234567) in title and description. but when i am trying fetch entity using

    $options['metadata_name_value_pairs'] = array(
                    array(
                            'name'  => 'action',
                            'value' => 'save'               
                    ),
                    array(
                        'name' => 'key',
                        'value' => '1234567'
                    ),
                );
       
     $entities = elgg_get_entities_from_metadata($options);

    it is giving me blank title and description, i dont know what's wrong here.

    Help please!

  • What do you mean with "i am passing same key(1234567) in title and "description".   Can you paste your code here in order to see your problem.

  • $key = '1234567';

    CASE 1:

    $flock= new ElggFlock();
    $flock->subtype = 'flock';
       
    $flock->access_id = 2;
    $flock->title = $key;
    $flock->description = $key;
    $flock->action = 'search';
    $flock->key = $key;
       
    if (!$flock->save()) {
      $error = elgg_echo('flock:error:cannot_save');
    }

    CASE 2:

    $flock= new ElggFlock();
    $flock->subtype = 'flock';
      
    $flock->access_id = 2;
    $flock->title = $key;
    $flock->description = $key;
    $flock->action = 'save';
    $flock->key = $key;
      
    if (!$flock->save()) {
      $error = elgg_echo('flock:error:cannot_save');
    }

    For a second case it not saving title and description. I dont know why. That i came to know when i am searching entity using metadatas.

    $options['metadata_name_value_pairs'] = array(
                    array(
                            'name'  => 'action',
                            'value' => 'save'              
                    ),
                    array(
                        'name' => 'key',
                        'value' => '1234567'
                    ),
                );

    $entities = elgg_get_entities_from_metadata($options);

    It returns entity with blank title and description.

  • Ok, some remarks here:

    • If  case 2 does not save ElggFlock entity, how can you expect that elgg_get_entities_from_metadata will find it:

    $options['metadata_name_value_pairs'] = array(
                    array(
                            'name'  => 'action',
                            'value' => 'save'              
                    ),
                    array(
                        'name' => 'key',
                        'value' => '1234567'
                    ),
                );

     

    • $entities is an arry of objects. Do you access it correctly? for example, if it returns only one object, then $entities[0] is this object.  Put some debug code to check this out:

    $entities = elgg_get_entities_from_metadata($options);

    echo 'Entities no: ' . count($entities). '<br>';
    foreach($entities as $entity) {
         echo 'Guid: '.$entity->guid.' Title: '.$entity->title.' Description: '
          .$entity->description.'<br>';
     }

    Also, you can just print out the entire object array:

    print_r($entities);

     

    • Check: use ElggObject instead of ElggFlock. This should create an ElggObject with subtype flock.  This because you might have a problem in your ElggFlock class itself.

     

  • Title and description are not metadatas