我的Laravel控制器中有这个方法:
public function getUserOrders($userId) :array{
$results = $this->_em->createQueryBuilder()
->select('Orders.orderItemCount')
->from($this->entityClass, 'Orders')
->Where("Orders.orderItemCount > '0'")
->andWhere("Orders.orderTotalPrice > '0'")
->andWhere("Orders.usersUserId = '{$userId}'")
->getQuery()->getArrayResult();
return $results;
}
我想更改$ userId可选的方法,因此在这种情况下不需要最后一个条件:
public function getUserOrders($userId = NULL) :array{
$results = $this->_em->createQueryBuilder()
->select('Orders.orderItemCount')
->from($this->entityClass, 'Orders')
->Where("Orders.orderItemCount > '0'")
->andWhere("Orders.orderTotalPrice > '0'")
if (!is_null($userId))
->andWhere("Orders.usersUserId = '{$userId}'")
->getQuery()->getArrayResult();
return $results;
}
我试图弄清楚实现这一目标的最佳方法是什么。有什么建议吗?
答案 0 :(得分:0)
试试这个
public function getUserOrders($userId = NULL) :array{
$results = $this->_em->createQueryBuilder()
->select('Orders.orderItemCount')
->from($this->entityClass, 'Orders')
->Where("Orders.orderItemCount > '0'")
->andWhere("Orders.orderTotalPrice > '0'");
if (!is_null($userId)){
$results->andWhere("Orders.usersUserId" = $userId);
}
$results->getQuery()->getArrayResult();
return $results;
}