0

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"?

  • 1
    Yes, it is faster. No, we will not translate it for you. You may try to 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. Certainly db_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
  • @Mołot It's nothing wrong with the "db_select", it's working very well; just trying to see if it will be significantly faster with "db_query". How "kpr((string)$child)" is helping? – GHG Jul 10 '14 at 12:27
  • Casting to string outputs a query string in readable SQL, and kpr is a devel function to put things on screen. So it helps to see what database is exactly asked to do. – Mołot Jul 10 '14 at 12:28
0

db_query can be used with an actual MySQL query - you just insert the MySQL query as you would in, say, PHPMyAdmin or some other UI for MySQL (or command line).

You can also substitute arguments into the query based on the second argument passed to the function, as such (found on Drupal.org as an example):

$result = db_query('SELECT n.nid, n.title, n.created FROM {node} n WHERE n.uid = :uid', array(':uid' => $uid));

Here the $uid variable is being passed as an argument to the query. Theoretically you could also write this as such:

$result = db_query('SELECT n.nid, n.title, n.created FROM {node} n WHERE n.uid = ' . $uid);

As Clive mentions in the comments though, this is just for illustrative purposes, as it does open you up to potential attack.

  • 2
    If you remove the last bit (the bit that makes you open to SQL injection) this is a good answer ;) – Clive Jul 9 '14 at 14:37
  • I pointed out that it was just for additional information, and that the OP shouldn't optimally do that. – Jack Ryan Jul 9 '14 at 14:39
  • 1
    Still, teach a man to fish and he'll eat, etc, etc...teach a man to fish with a poisoned net and everyone dies :p – Clive Jul 9 '14 at 14:41

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.