Doubt in elgg_get_entities_from_metadata()

Hi guys,

Please guide me to execute query.

i have two entity objects "object1", "object2".

both objects has one metadata with different values.

eg: object1->data and object2->data

value of object1->data = "alarm,walk,school,home,sleep"; 

value of object2->data = "wake up,walk,study,home,play";  

i want to get all object2 which has any single value matched with object1->data.

here "walk" and "home" is common value in object1 and object2.

Please give me suggestions to do this.

  • what is type and subtype of object1 and object2 ?  I assume its same.

    my answer is use LIKE query.

        $dbprefix = elgg_get_config("dbprefix");

          $options['joins'][]  = "JOIN {$dbprefix}metadata prio ON e.guid = prio.entity_guid";

        $options['joins'][]  = "JOIN {$dbprefix}metastrings prio_name on prio.name_id=prio_name.id";

        $options['joins'][]  = "JOIN {$dbprefix}metastrings prio_value on prio.value_id=prio_value.id";

        $options['wheres'][] = "prio_name.string = 'data'";

     $options['wheres'][] = "prio_value.string LIKE  '%alarm%'   OR  prio_value.string LIKE  '%walk%'  ";

  • Thanks for your reply enRaiser. 

    Let me try this.

  • object1->data = "alarm,walk,school,home,sleep"; 

    Instead of storing it like that you should store it like this:

    object1->data = array(
        'alarm',

        'walk',
        'school',
        'home',
        'sleep'
    );

    Then you can properly use the options of elgg_get_entities_from_metadata()

    $options = array(
        'type' => 'object',
        'subtype' => 'subtype',
        'metadata_name_value_pairs' => array(
            'name' => 'data',
            'value' => array('walk', 'alarm')
        )
    );

  • Thanks for your help Matt Beckett. 

    I used this code, but unfortunately its not working.

    Instead i used this code for fetching and its working,

    $search = array('walk', 'alarm');

    $query = array();

    foreach ($search as $sec) {

        $query[] = array('name' => 'data', 'value' => '%'.$sec.'%', 'operand' => 'LIKE', 'case_sensitive' => false );

     };

    $object2 = elgg_get_entities_from_metadata(array(
                'type'=>'object',
                'subtype'=>'object2',
                'metadata_name_value_pairs' => $query,
                'metadata_name_value_pairs_operator' => 'OR',
      ));

    And i have one more doubt:

    Is my code affect the performance of query execution time when both "object2" entities and $search array increases? 

  • That works too - my code wasn't tested, it should work something like that - maybe it's pluralized 'values'

    Anyway, yes, performance will decrease the more search fields you add in.  If you're building an app that needs to search on a lot of metadata simultaneously I'd recommend offloading search to solr or something.  MySQL doesn't do the best job in that regard