usage of elgg_get_entities_from_metadata()

Do I get this function right? I can't get what I want.   

$data_list = elgg_get_entities_from_metadata(array('types'=>'object','subtypes'=>'selldata', 'limit'=>1, 'metadata_name_value_pairs' =>array('name'=>'month', 'value' => 2010-03)));

Or multiple

$data_list = elgg_get_entities_from_metadata(array('metadata_names'=>array('month','name' 'metadat_values'=>array('2010-03','John')));

  • There are a few problems.  First, you need to quote '2010-03' or PHP will interpret it as 2007 (2010 *minus* 03).  Since you are only using one metadata name/value pair, you also should change that call to:

    'metadata_name_value_pair' => array('name' => ...)

    or:

    'metadata_name_value_pairs' => array(array('name' => ...))

     

    The second statement has a typo in the 'metadata_values' field, and probably doesn't do exactly what you want because name isn't available metadata for a user.  (Using multiple names and values doesn't do what you want in this case anyway--It's an OR statement instead of an AND.  Use metadata_name_value_pairs.)  I'm not sure what you want the second call to return...

    You might want to review the documentation on the metadata system: http://docs.elgg.org/wiki/Engine/DataModel/Metadata

  • Brett, I finnally get it works like this:

     elgg_get_entities_from_metadata(array('types'=>'object', 'subtypes'=>'selldata', 'limit'=>1, 'metadata_names' =>array('name','month'), 'metadata_values' =>array($name,$month), 'owner_guid'=>get_loggedin_userid()));

    Thanks.

  • Glad it's working for you but there are a still a few problems:

    First, this statement will fail if a non-logged in user looks at that page--It will either return nothing or return all matches ignoring the owner.  Make sure you put a check to see if the user is logged in before calling this function.

    Second, use metadata_name_value_pairs.  This statement doesn't do what you want and only works by chance.  The SQL generated by this statement will be something like:

    ... WHERE metadata_name IN ('name', 'month') AND metadata_value IN ('$name', '$month') ...

    You want SQL to be:

    ... WHERE (metadata_name1 = 'name' AND metadata_value1 = '$name') AND (metadata_name2 = 'month' AND metadata_value2 = '$month)

    A name/value pair will look like:

    'metadata_name_value_pair' => array(

        array('name' => 'name', 'value' => $name),

        array('name' => 'month', 'value' => $month)

    )

    ...and will generate what you want.

     

  • Thanks your suggestion and guidance, Brett.

  • I found this function not work indeed.

    for example: if there are 3 entities, only one entity match what I need, and the other two are empty. the function will also return 3 result, but the other two will use the value of the match one.

    I had even tested Brett's suggestion, not work too:

    'metadata_name_value_pair' => array(

        array('name' => 'name', 'value' => $name),

        array('name' => 'month', 'value' => $month)

    )

     

  • The following is my current codes:

     elgg_get_entities_from_metadata(array('types'=>'object', 'subtypes'=>'selldata', 'limit'=>1, 'metadata_names' =>array('name','month'), 'metadata_values' =>array($name,$month), 'owner_guid'=>get_loggedin_userid()));

    It seems that either $name or $month matches, the results will be returned.

    If the datas are:

    name | sell | month
    John | 200 | 2010-03
    Simon| 100 | 2010-03
    King | 300 | 2010-03
    John | 123 | 2010-02

    if I want to get entity with $name='John' and $month='2010-03', the result returns:

    sell:200
    sell:200
    sell:200

  • @jdleung - It's very hard to help debug this because we don't know how your plugin saves its data.  Can you post the code you're using to save information?

  • Brett, here is it

    // get the form input
    $name = get_input('name');
    $month = get_input('month');
    $sell = get_input('sell');
    $pv = get_input('pv');
    $i=0;
      
    foreach($name as $item){

        // create a new blog object
        $insertdata = new ElggObject(); 
       
        $insertdata->title = $name[$i];
        $insertdata->name = $name[$i];
        $insertdata->description = $month;
        $insertdata->month = $month;
        $insertdata->sell = $sell[$i];
        $insertdata->pv = $pv[$i];
        $insertdata->subtype = "selldata";

        // for now make all blog posts public
        $insertdata->access_id = 0;

        // owner is logged in user
        $insertdata->owner_guid = get_loggedin_userid();

        // save to database
        $insertdata->save();
       
        $i++;
    }

    $name, $sell and $pv are array, since the design is one form for multi input.

  • It takes me a long time to get it really works now, a 'operand' => '=' is needed!

    'metadata_name_value_pair' => array(

        array('name' => 'name', 'value' => $name, 'operand' => '=' ),

        array('name' => 'month', 'value' => $month, 'operand' => '=' )

    )

  • I realize this thread is old - but it is one of the top google results on this topic.  The code for v1.7.8 should be "metadata_name_value_pairs" not "metadata_name_value_pair".