0

Checked for this, but don't seem to be any specific answers to this question.

I'm trying to pass results from EntityFieldQuery to a db_select query, via a string. However, although debugging both queries with set values shows that they are operating correctly and providing the desired results, when using the string in db_select I am getting null results.

The purpose is to get certain node ID's as an integer value and pass them as an array of Entity_ID integer values, so the db_select function only selects table rows which match a certain Entity_ID.

I presume I'm making a output format error for the EntityFieldQuery, but can't work out what exactly it is. Code is as follows:

// EntityFieldQuery
$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
                   ->entityCondition('bundle', 'contenttype1')
                   ->propertyCondition('status', 1)
                    ->fieldCondition('field_field', 'value', $contenttype2, '=')
                   ->fieldOrderBy('field_field', 'value', 'ASC')
                    ->execute();

// Debugging functions only, return correct values when tested
$show = array_keys($entities['node']);
echo 'this is show: ' . $show;

$print = var_dump($show);
echo 'this is print: ' . $print;

// db_select function, returns correct sum when '$entities' is replaced with integer value for entity_id
$dbquery = db_select('tablename', 't')
  ->condition('t.entity_id', '$entities', '=');
$dbquery->addExpression('SUM(value)', 'value');

$result = $dbquery->execute()->fetchField();

// Debugging function only
print 'this is result: ' . $result;

The EntityFieldQuery is producing the correct values, but it isn't clear that they are in the correct format. The $print output is:

array(2) { [0]=>int(213) [1]=>int(214) }

At present, the SUM result works correctly when I replace '$entities' in the db_select with (e.g.) '213', but returns 0.0 when I use '$entities'.

EDIT: using

dpq($dbquery); 
dpr($result);

Gives the confirm message:

SELECT SUM(value) AS value
FROM 
{votingapi_vote} v
WHERE  (v.entity_id = ) 

And the error message:

Warning: PDO::quote() expects parameter 1 to be string, array given in dpq() (line 1835 of /var/www/html/sites/all/modules/devel/devel.module).

What is the output format code / function I should be using for EntityFieldQuery?

0

Try this approach. after getting your entities using EntityFieldQuery, create a for loop in order to check the val that was stored in your variable $entities. Then use dsm or dpm to check if it returns a result.

$results = '';
for ($i=0; $i < count($your_entity) ; $i++) { 
    $query = db_select('yourtablename', 't');
    $query->fields('t');
    $query->condition('t.entity_id', $your_variable);
    $results = $query->execute()->fetchAll();
}
dsm($results);

Hope this helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.