[support] db_select()->condition()

Metzler, David metzlerd at evergreen.edu
Thu May 24 20:56:26 UTC 2012


Two things: 

1.  Don't use db_select just because you're converting a module.  You
are adding overhead that doesn't need to be there. Event the authors of
DBTNG agree with this statement.  Stick to db_query unless you need to
generate SQL dynamically. 

2. Change the variable prior to binding it.  Don't do token replacement
at all.  The idea is that the second parameter of the condition method
should be that variable that contains the array, not a token replacement
thing so if you're trying to make a wild card like, just concat like
this: 

db_select('mytable', 'mt')
  ->fields('mt', array('myvar'))
  ->condition('mystring', '%' . $somrvariable . '%', LIKE)
  ->execute();

Dave
-----Original Message-----
From: support-bounces at drupal.org [mailto:support-bounces at drupal.org] On
Behalf Of Earnie Boyd
Sent: Thursday, May 24, 2012 1:28 PM
To: support at drupal.org
Subject: [support] db_select()->condition()

I have an existing module I'm trying to convert to DBTNG and have a
condition I need a suggestion on.

Original statement is something like
SELECT mt.myvar FROM {mytable} mt WHERE mystring LIKE '%%%s%%';

I want to change it to
db_select('mytable', 'mt')
  ->fields('mt', array('myvar'))
  ->condtion('mystring', '%:mystring%', LIKE)
  ->execute();

The problem is condition() only takes three parameters, what would you
suggest to use to replace :mystring?  I'm using t() but that seems
like a misuse of it.

-- 
Earnie
-- https://sites.google.com/site/earnieboyd
-- 
[ Drupal support list | http://lists.drupal.org/ ]


More information about the support mailing list