Converting db_rewrite_sql for D7

View: New views
6 Messages — Rating Filter:   Alert me  

Converting db_rewrite_sql for D7

by nan wich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

For the most part I am finding converting my queries fairly straightforward. But I just ran into one that I'm having a bit of trouble with:
ORDER BY ' . ($filters['block_type'] == 0 ? 'RAND()' : 'n.created DESC')
 
Obviously the one case will end up as "->orderBy('n.created', 'DESC')", but there's no column for RAND. Do I just make one up (maybe nid)?
 
 
Nancy E. Wichmann, PMP
Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
 

Re: Converting db_rewrite_sql for D7

by Karoly Negyesi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

->orderRandom()

Re: Converting db_rewrite_sql for D7

by nan wich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Karoly Negyesi wrote:
> ->orderRandom()

Thanks. Darn, that means adding an "if".

Nancy E. Wichmann, PMP
Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King,
Jr.


Re: Converting db_rewrite_sql for D7

by nan wich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Does this look reasonable (sorry, I'm not far enough along to actually
test):

$where[] = sprintf('(r.rid IN (' . $filters['rid_filter']) . ' OR (%d IN ('
. $filters['rid_filter']) . ' AND n.uid = 0))', DRUPAL_ANONYMOUS_RID);

becomes:

$query->condition(db_or()->condition('r.rid', $filters['rid_filter'],
'IN')->condition(DRUPAL_ANONYMOUS_RID, $filters['rid_filter'],
'IN')->condition('n.uid', 0);


Nancy E. Wichmann, PMP
Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King,
Jr.


Re: Converting db_rewrite_sql for D7

by Larry Garfield :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Saturday 10 October 2009 5:18:31 pm Nancy Wichmann wrote:

> Does this look reasonable (sorry, I'm not far enough along to actually
> test):
>
> $where[] = sprintf('(r.rid IN (' . $filters['rid_filter']) . ' OR (%d IN ('
> . $filters['rid_filter']) . ' AND n.uid = 0))', DRUPAL_ANONYMOUS_RID);
>
> becomes:
>
> $query->condition(db_or()->condition('r.rid', $filters['rid_filter'],
> 'IN')->condition(DRUPAL_ANONYMOUS_RID, $filters['rid_filter'],
> 'IN')->condition('n.uid', 0);

It's easier to follow if you use the common line breaking convention.  I also
find it easier to build the OR statement separately most of the time rather
than trying to inline everything, even though it is possible to inline it all.

Also, according to my IDE both of the above statements are syntax errors. :-)  
I think you're missing parentheses somewhere.  Assuming that this is the
original query (converted to double-quote syntax because it's easier to read
in this case):

$where[] = sprintf("(r.rid IN ({$filters['rid_filter']}) OR (%d IN
({$filters['rid_filter']}) AND n.uid = 0))", DRUPAL_ANONYMOUS_RID);


Then this, I believe, would be the DBTNG version:

$and = db_and()
  ->condition('n.uid', 0)
  ->condition(DRUPAL_ANONYMOUS_RID, $filters['rid_filter'], 'IN');
$or = db_or()
  ->condition('r.rid', $filters['rid_filter'], 'IN')
  ->condition($and);
$query->condition($or);

Which, if you don't want the intermediate variables, would then be:

$query->condition(db_or()
  ->condition('r.rid', $filters['rid_filter'], 'IN')
  ->condition(db_and()
    ->condition('n.uid', 0)
    ->condition(DRUPAL_ANONYMOUS_RID, $filters['rid_filter'], 'IN')
  )
);

(Personally I prefer the first version as I find it easier to read and edit
later, but both should work.)


--
Larry Garfield
larry@...

Re: Converting db_rewrite_sql for D7

by nan wich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Larry Garfield wrote
> It's easier to follow if you use the common line breaking convention.
> I also find it easier to build the OR statement separately ... rather
> than trying to inline everything,

Thank you so much, Larry.

I like the intermediate version much better. Not only is it more readable,
but makes more sense. I did it like that because that's the way it is
documented on DO (perhaps it should be updated with this type of syntax).

And, yes, when I got the changes made and tried to enable it, there was a
syntax error.

Nancy E. Wichmann, PMP
Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King,
Jr.