1
我正在爲我的大學課程製作汽車預訂應用程序。我有預留存儲在分貝,我想查詢在特定時間段內ara可用的汽車。學說 - 不在
所以,我創建了自定義庫類,看起來像這樣:
use Doctrine\ORM\EntityRepository;
class CarRepository extends EntityRepository
{
public function findAllAvailable($pickupDate, $returnDate)
{
$subQueryBuilder = $this->getEntityManager()->createQueryBuilder();
$carsInOverlapingPeriod = $subQueryBuilder
->select('rv')
->from('AppBundle:Reservation', 'rv')
->where(':pickup_date < rv.returnDateTime AND :return_date > rv.pickupDateTime')
->setParameters(array(
'pickup_date' => $pickupDate,
'return_date' => $returnDate
))
->getQuery()
->getArrayResult()
;
$queryBuilder = $this->getEntityManager()->createQueryBuilder();
$query = $queryBuilder
->select(['car'])
->from('AppBundle:Car', 'car')
->where($queryBuilder->expr()->notIn('car.id', ':subQuery'))
->setParameter('subQuery', $carsInOverlapingPeriod)
->getQuery()
;
return $query->getResult();
}
}
$ carsInOverlapingPeriod返回給定的時間段中發生的保留的陣列,$查詢應該只返回車可用。
在保留表中,我有名爲car_id的字段/列(在保留實體中,它是$ carId)。
無法弄清楚如何在$ query中構建'where'條件。
我想這應該是這個樣子:
WHERE car.id NOT IN subquery WITH car.id = rv.car_id
當我嘗試做rv.car_id我獲取錯誤: '[語義錯誤]行0,col 10附近'car_id FROM AppBundle:Reservation':錯誤:類AppBundle \ Entity \ Reservation沒有字段或關聯名爲car_id' 而且當我嘗試rv.carId I在'carId FROM AppBundle:Reservation'附近得到'[Semantical Error]行0,col 10':Er ror:PathExpression無效。必須是StateFieldPathExpression.' – jjpawlica
嗨@jjpawlica對不起,我不知道你的實體的模型定義,所以我不知道汽車id列的真實姓名,而不是這個嘗試與'IDENTITY(rv.carId) ' – Matteo
Reservation Entity field: '/ ** * @ORM \ ManyToOne(targetEntity =「Car」,inversedBy =「reservations」) * @ORM \ JoinColumn(name =「car_id」,referencedColumnName =「id」) */ 保護$ carId;' – jjpawlica