Corma supports a simple array-based query syntax everywhere that a where query is accepted. The array keys define the column, while the value specifies the value to query for. The column can optionally declare a comparison operator (=, <, >, <=, >=, <>, !=, LIKE). If an array is specified for the value it translates to an IN() clause.
$objects = $orm->findBy(MyObject::class, ['columnA'=>'A', 'columnB >'=> 10, 'columnC' => null, 'inColumn'=>[1,2,3]]);
Would translate to roughly the following SQL:
SELECT * FROM my_objects main WHERE columnA = 'A' AND columnB > 10 AND columnC IS NULL AND inColumn IN(1,2,3)
While this handles the most common use cases, you will need to work with the Doctrine QueryBuilder directly for the following scenarios:
- Joins
- You need an OR in your where clause
- You need to query for more than one condition on the same column
- Group by statements
- Having statements
In that case you can use the Corma QueryHelper to start your query, and then manipulate the QueryBuilder in your repository methods.
/**
* @param string $value
* @return MyObject[]
*/
public function findBySomething($value)
{
$qb = $this->queryHelper->buildSelectQuery($this->getTableName(), 'main.*', ['a.columnName'=>$value])
$qb->join('main', AnotherObject::getTableName(), 'a', 'main.id = a.myObjectId')
->groupBy('main.id');
return $this->fetchAll($qb);
}