I am using "db_select" in a computed field to get an array of values from database (trough an entity reference field - multi value):
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$current_nid = $wrapper->nid->value();
$values_y = array();
/* First I get an array of referenced nodes IDs that are neccesary in second query*/
$child = db_select('field_data_field_plan_tpl_select_trainings', 'f')
->fields('f', array('field_plan_tpl_select_trainings_target_id'))
->condition('entity_type', 'node')
->condition('bundle', 'skills_plan_tpl')
->condition('entity_id', $current_nid)
->execute();
$child_nid = array();
foreach ($child as $item_child){
$child_nid[] = $item_child->field_plan_tpl_select_trainings_target_id;
}
/* Here I get an array of values from referenced nodes */
$week_no = db_select('field_data_field_skill_week_no_planned', 'f')
->fields('f', array('field_skill_week_no_planned_value'))
->condition('entity_type', 'node')
->condition('bundle', 'skill')
->condition('entity_id', array($child_nid))
->execute();
$week_value = array();
foreach ($week_no as $item_week){
$week_value[] = $item_week->field_skill_week_no_planned_value;
}
$values_y = array_filter($week_value);
I understand that "db_query" is faster than "db_select"; is it true and can it have a positive impact in my situation? How can this be translated into "db_query"?
kpr((string)$child)
before you->execute()
it. Positive impact? Depends on what you want. You haven't said what's wrong with the way it is now. Certainlydb_query
allows to make mistakes easier and makes it harder for you to debug them - nothing positive about that. – Mołot Jul 9 '14 at 14:34