spotorm / spot2

Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer
http://phpdatamapper.com
BSD 3-Clause "New" or "Revised" License
601 stars 101 forks source link

Group where and orWhere #218

Closed keevitaja closed 7 years ago

keevitaja commented 7 years ago

Hello,

is it possible to group where and orWhere without using raw queries?

where a = 1 and (where b = 8 or where c = 4)

Arzaroth commented 7 years ago

You can achieve this doing :

$mapper->where(['a' => 1])->andWhere(['b' => 8, 'c' => 4], 'OR');
Kipperlenny commented 5 years ago

Any solution for:

where a = 1 and (where b LIKE "8 %" or where b LIKE "% 8 %")

?

FlipEverything commented 5 years ago
$mapper->where(['a' => 1])->andWhere(['b :like' => '8 %', 'b :like' => '% 8 %'], 'OR');
Kipperlenny commented 5 years ago

Are you sure? An array with the same key twice?

FlipEverything commented 5 years ago

Sorry, my bad... You can try this workaround:

Example code:

$query = $mapper->select()->noQuote()->where(['name' => 1])->andWhere(['email :like' => '8 %', 'email  :like' => '% 8 %'], 'OR');
echo($query->toSql()).PHP_EOL;
var_dump($query->getParameters());

Output:

SELECT * FROM x WHERE (x.name = ?) AND (x.email LIKE ? OR x.email LIKE ?)
array(3) {
  [1] =>
  int(1)
  [2] =>
  string(3) "8 %"
  [3] =>
  string(5) "% 8 %"
}

Ofc you can restructure your request or do a regex instead etc.

Kipperlenny commented 5 years ago

Thank you very much! I used another option: created my own operator.