views:

410

answers:

1

Hi,

How do I write the following MySQL query using Doctrine's findBy*() method?:

SELECT column_name1, column_name2 FROM table_name
WHERE column_name3 LIKE '%search_key%';

For Example, to fetch multiple rows from a column named "ColumnName" (below) using Doctrine:

$users = Doctrine::getTable('User')->findByColumnName('active');

echo $users[0]->username;
echo $users[1]->username;

I tried:

$search_key = 'some value';
$users = Doctrine::getTable('User')->findByColumnName('%$search_key%');

    echo $users[0]->username;
    echo $users[1]->username;

and I got no errors, but nothing displayed.

Any assistance will be really appreciated. Thanks in advance.

+2  A: 
$users = Doctrine::getTable('User')->createQuery('u')
  ->where('column_name3 LIKE ?', '%search_key%')
  ->execute();
Coronatus
+1. Nice, thanks Coronatus
01010011
@Coronatus, if I have a variable named $search_key that contains the value of something to search, how do I replace %search_key% with %$search_key% in my query?
01010011
Learn the basic syntax FFS. `'%'.$search_key.'%'`
Coronatus
@Coronatus, ok thanks for the tip!
01010011
@Coronatus, nice thanks it worked!
01010011