QueryBuilder and sub-conditions
Hi I am trying to build a query using QueryBuilder
and Query\Expr
. My problem is the assembly substring, for example to get a list of models for all these conditions?
-
Status
true
, not in themodel
name list -
Status
false
and not in anothermodel
name list
Code:
public function process(QueryBuilder $qb)
{
$cond1 = new Expr\Andx;
$cond1->add($qb->expr()->eq('status', 0);
$cond2= new Expr\Andx;
$cond2->add($qb->expr()->notIn('model', array('308','408'));
$cond1->add($cond2);
$cond3 = new Expr\Andx;
$cond3->add($qb->expr()->eq('status', 1);
$cond4= new Expr\Andx;
$cond4->add($qb->expr()->notIn('model', array('A1','A2'));
$cond3->add($cond4);
$qb->andWhere($cond1);
$qb->andWhere($cond3);
}
Model class
Class Vehicle{
/**
* @var string
*
* @ORM\Column(name="model", type="string", length=255, nullable=true)
*/
private $modele;
/**
* @var string
*
* @ORM\Column(name="make", type="string", length=255, nullable=true)
*/
private $make;
/**
* @var boolean (status for vehicule 1 = Used vehicle)
*
* @ORM\Column(name="status", type="boolean")
* @Serializer\Groups({"list", "details"})
*/
private $status;
//-- Other properties, getter & setter
}
thanks for the help
+3
source to share
2 answers
$qb->andWhere(
$qb->expr()->andx(
$qb->expr()->andx(
$qb->expr()->eq('status', 0),
$qb->expr()->andx(
$qb->expr()->notLike('model', '308'),
$qb->expr()->notLike('model', '408')
)
),
$qb->expr()->andx(
$qb->expr()->eq('status', 1),
$qb->expr()->andx(
$qb->expr()->notLike('model', 'A1'),
$qb->expr()->notLike('model', 'A2')
)
)
)
);
You can work with nesting expressions as long as you read the docs
Let me know if you need help :)
+2
source to share