[support] Converting a query to D7

Michael Prasuhn mike at mikeyp.net
Wed Oct 12 07:36:10 UTC 2011


Dipen wrote:
> http://upgrade.boombatower.com/tools/sql/inline
> 
> db_select('user_badges_roles', 'ubr')
>   ->fields('ubr', array('rid', 'ubb'))
>   ->join('user_badges_badges', 'ubb', 'ubb.bid = ubr.bid')
>   ->condition('ubr.rid', '@todo Set the appropriate argument.')
>   ->execute();

1. the fields section is plainly incorrect as it won't select any fields
from user_badges_badges and has an erroneous field from user_badges_roles

2. Join just returns the tables alias, not a select query object, so
it's not chainable.

Vaibhav Jain wrote:
> You can use this query like this
> $query = db_select('user_badges_roles', 'ubr')
>   ->fields('ubr', array('rid', 'ubb'))
>   ->join('user_badges_badges', 'ubb', 'ubb.bid = ubr.bid');
>
> $query = condition('ubr.rid', '@todo Set the appropriate argument.')
>   ->execute();

Same problem as number 1 above, and what on earth is the condition
function? I know of no procedural function in Drupal by that name.


Nancy, here's what you want:

$query = db_select('user_badges_roles', 'ubr');
$query->innerJoin('{user_badges_badges', 'ubb', 'ubb.bid = ubr.bid');
$result = $query->fields('ubr', 'rid')
  ->fields('ubb')
  ->condition('ubr.rid' $YOUR_ARGUMENT)
  ->execute();

There may be a more elegant way to get structure the query, but this
will get you the results you're looking for.

-Mike

__________________
Michael Prasuhn
mike at mikeyp.net



More information about the support mailing list