Getting entities GUIDs from string array and anotating main object

This might sound complicated because it is, to me anyway. Any suggestions are appreciated.

I have array of strings (using var_dump):

array(3) { [0]=> string(9) "655-2032B" [1]=> string(8) "655-2056" [2]=> string(9) "300-85091" }

That array comes from  $attachment[] = $core_title->number; code below.

foreach ($data->parts as $core_title) {

    $attachment[] = $core_title->number;
    $q = $core_title->number;

// Run query
    $dbprefix = elgg_get_config('dbprefix');
    $options['joins'][] = "JOIN {$dbprefix}objects_entity oe ON oe.guid = e.guid";
    $options['wheres'][] = "oe.title = '$q'";
    $options['types'] = array('object');
    $options['subtypes'] = array('core');
    $options['limit'] = 1;

    $entity = elgg_get_entities($options);

// I got that entity that matches the title ($core_title->number)

    //Now get GUID of the entity
    foreach ($entity as $o) {
        $coreguid = $o->guid;
        $coreguides[] = $o->guid;


    }

  }

It works I get the GUID as $coreguid but only for the first item in original array

I to save EACH item from array in $data->parts (going through process of finding GUID of entity that matches the title) using:

$obj->annotate('cores', $coreguid);

Unfortunately it only gets first item from original array.

   var_dump($attachment); returns this (gets all 3 items):

array(3) { [0]=> string(9) "655-2032B" [1]=> string(8) "655-2056" [2]=> string(9) "300-85091" }

   var_dump($coreguides); returns this (only 1, first one):

array(1) { [0]=> int(23314) }

  • You're setting the SQL LIMIT to 1.

    Tip: don't using singular words like $entity to name arrays. More importantly, you're placing $q in SQL without escaping. You want something like:

    $escaped_q = sanitize_string($q);
    $options['wheres'][] = "oe.title = '$escaped_q'";
  • @steve_clay

    Thank you for your help first of all.

    Reason SQL LIMIT is 1 is because there is only 1 entity with that exact title (655-2032B, 655-2056 or 300-85091) that are in original array and SQL query is inside foreach code. I tried setting it to 0 and still same result.

    Same with using  $escaped_q = sanitize_string($q); returns same result.

    I think I'm doing something wrong with "foreach", This is what I'm trying to do and I think I'm doing in the code but not quite it seems like.

    Original data comes from Json ( $data = json_decode($json); )

    Inside foreach ($data->parts as $core_title) {)  which are 655-2032B, 655-2056 and 300-85091 so using $q = $core_title->number; ) gets all 3 numbers (it should as I'm using foreach)

    Inside that I get entity for each of those numbers using SQL query and elgg_get_entities (those numbers correspond to entity title so it finds them)

    Then I get GUID for each of the entities inside that using

    foreach ($entity as $o) { $coreguid = $o->guid; $coreguides[] = $o->guid; }

    So I think I need another foreach maybe to get all 3 numbers in foreach ($data->parts as $core_title) { processed by elgg_get_entities that matches number to each entity title and pulls GUID of these found entities.

    Code only gets first one 655-2032B and pulls its GUID which is 23314

    That's why I'm looking at var dump results of the code:

    var_dump($attachment); returns  all 3 items:

    array(3) { [0]=> string(9) "655-2032B" [1]=> string(8) "655-2056" [2]=> string(9) "300-85091" }

       var_dump($coreguides); returns only 1, first one:

    array(1) { [0]=> int(23314) }

  • Thing that makes me believe it is my foreach loops is because if I only use this:

    foreach ($data->parts as $core_title) {
        $attachment = $core_title->number;  
    
        $obj->annotate('cores', $attachment);
        }

    it creates annotation with each number as value. (655-2032B, 655-2056 and 300-85091)

    But I need to find object that has title as each number first and then annotate with GUID of that object for each number.

  • Explain again, what you try to do?

    655-2032B, 655-2056 and 300-85091

    What is it: title or metadata?

    Do you want to search a part of title, or get the entities by value?