DCO

Applications are open for the 2015 Spring session of Drupal Career Online (DCO) program. Classes Start March 10! Learn more.

New Podcast

Posted Wednesday, March 25 at 4:48 pm
Adam Hoenich (phenaproxima), lead author and maintainer of the Drupal Module Upgrader joins Ryan, Ted, and Mike to talk about moving modules from D7 to D8, MidCamp, D8 Accelerate matching funds, ranking Drupal support mechanisms, and Ted's personal phone number. We also have some great picks of the week, and plenty of information on the upcoming Florida DrupalCamp 2015!
Download Podcast 147
DrupalEasy_ep147_20150323.mp3
Syndicate content

Everywhere

Connect with DrupalEasy:

Survey

Interested in learning more about Drupal, but can't find the training you're looking for? Help us help you by letting us know what workshops interest you. Take this short survey to let us know!

Newsletter

Stay up-to-date on DrupalEasy workshops and other events. On average, we send about 1-2 newsletters per month.

Who are we?

DrupalEasy is a training and consulting company that specializes in public and private Drupal training as well as site building and consulting with a focus on best practices and building sustainable web sites. More...

What is Drupal?

Drupal is a free, super-powerful content management system for sites that require information posting and collection, including blogs, forums, videos, photos, and databases of information. We think it is the best platform available. Here's why...

Why Drupal?

More and more savvy organizations are going with Drupal for content management, and its no mystery why. It’s free, flexible, and easy to maintain for small or large volume sites. Learn more...

Arbitrary "OR" SQL Queries

I was working on a Drupal migration project today using the Migrate module where I needed to import only select user roles from the source (Drupal 6) database.

The Migrate module allows for a custom query to select only the user roles that need to be imported. In my case, the two roles I wanted to import had role IDs of 4 and 6. So, how do I write a query using the Drupal Database API to do this? Turns out there's a pretty elegant answer. Rather than writing something like:

SELECT * FROM role r WHERE rid=4 OR rid=6;

The proper way of writing the select query is:

$query = parent::query();
$ored = db_or();
$ored
->condition('rid', 4)
->condition('rid', 6);
$query->condition($ored);

Note the elegant "db_or()" function that returns a DatabaseCondition object. Add the two conditions to this object, and they're automagically "or"ed.

I discovered all this via this nice blog post by Rahul Singla.

Trackback URL for this post:

http://drupaleasy.com/trackback/565
Your rating: None Average: 4.7 (3 votes)

More Quicktips


2 comments

Guest wrote 2 years 1 day ago

IN?

Umm, doesn't
$query->condition('rid', array(4,6), 'IN');
do it?

But yes, the db_and and db_or methods are very useful.

Love your podcasts!

ultimike wrote 2 years 1 day ago

yup

Yep, "$query->condition('rid', array(4,6), 'IN');" will work as well, but for some reason, I find the db_or method a bit more elegant.

Thanks,
-mike

Syndicate content