adrienbrault/doctrine2 — pull request #1 by adrienbrault (2012-03-23 12:23:24+00:00)
Title: Expr\Composition instances can now have prioritized parts.

Hello world!

First let me explain the problem i wanted to solve.

We have many queries in our application, and we organized our repositories like that (simplified, but to illustrate the problem):

```php
createQueryBuilder('u');

$qb->andWhere($qb->expr()->eq('u.enabled', 1));

return $qb;
}

public function selectEnabledUserByIdQB($id)
{
$qb = $this->selectEnabledUserQB();

$qb ->andWhere($qb->expr()->eq('u.id', ':id'))
->setParameter('id', $id);

return $qb;
}
}
```

The problem is that ```selectEnabledUserByIdQB``` will generate an SQL query that looks like that (simplified again):

```sql
SELECT *
FROM users
WHERE
enabled = 1
AND id = 12345
```

If mysql is unable for some reason (like i encountered in complex queries), to optimize the query to first look up for the ```id``` and then the ```enabled``` column, the query will be slower than:

```sql
SELECT *
FROM users
WHERE
id = 12345
AND enabled = 1
```

My PR is trying to solve this problem.

What our repository now look like:

```php
createQueryBuilder('u');

$qb->andWhere($qb->expr()->eq('u.enabled', 1)); // We ommit the priority and it will default to 0

return $qb;
}

public function selectEnabledUserByIdQB($id)
{
$qb = $this->selectEnabledUserQB();

$qb ->andWhere(array($qb->expr()->eq('u.id', ':id'), 10)) // Set a high priority because this condition will use an index with a high cardinality
->setParameter('id', $id);

return $qb;
}
}
```

And what the SQL that ```selectEnabledUserByIdQB``` creates will look like:

```sql
SELECT *
FROM users
WHERE
id = 12345
AND enabled = 1
```

The order of conditions in the where is also very important with multi column index according to the MySQL doc :

This works on all Expr classes inheriting from Expr\Composition (ie: Expr\And & Expr\Or). This is also usefull in OR expressions.

I'm not familiar with tests but tried to code the mosts importants, comments are welcomed.

Mostly, what do you think of this approach ?