Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine - or where?

Tags:

php

doctrine

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'.

like image 576
Splendid Avatar asked Jul 29 '11 20:07

Splendid


People also ask

What is Doctrine in symfony?

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.

Is Query builder an ORM?

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.

How does query builder work?

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.

What is Query builder in C#?

SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird. sqlkata.com.


2 Answers

$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.

like image 78
Tom Avatar answered Sep 17 '22 13:09

Tom


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');
like image 41
adlawson Avatar answered Sep 16 '22 13:09

adlawson