I have the following query:
$query = Doctrine_Query::create()
->from('Member m')
->where("m.type='1'")
->andWhere("m.name LIKE '%$term%'")
->orWhere("m.surname LIKE '%$term%'")
->orWhere("m.company LIKE '%$term%'")
->orderBy('id DESC');
But it's not working like I want — it is ignoring type
column.
What I need is result set where m.type=1
and some of other fields in this query is LIKE 'something'
.
Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.
The ORM's query builder provides a simple to use fluent interface for creating and running queries. By composing queries together, you can create advanced queries using unions and subqueries with ease.
Query Builder is designed to enhance productivity and simplify SQL query building tasks. Query Builder provides a graphical user interface for creating SQL queries. You can drag-and-drop multiple tables, views and their columns onto a visual designer to generate SQL statements.
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird. sqlkata.com.
$query = Doctrine_Query::create()
->from('Member m')
->where('m.type = 1 AND m.name LIKE ?', '%'.$term.'%')
->orWhere('m.type = 1 AND m.surname LIKE ?', '%'.$term.'%')
->orWhere('m.type = 1 AND m.company LIKE ?', '%'.$term.'%')
->orderBy('m.id DESC');
Your OR conditions didn't include the first condition. It's also recommended to use the ?
for your variables to ensure Doctrine escapes them.
Tom's answer is correct, although I like to keep code repetition/duplication to a minimum.
This way should also work, while being a shorter, cleaner way to do it
$query = Doctrine_Query::create()
->from('Member m')
->where('m.type = ?', 1)
->andWhere('m.name LIKE :term OR m.surname LIKE :term OR m.company LIKE :term', array(':term' => '%' . $term . '%'))
->orderBy('m.id DESC');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With