我有两个实体Reservation
,这是像优步一样的预订。预订中包含字段pickUpAdress
和字段dropOffAdress
:
预留:
/**
* @Assert\Valid()
* @ORM\ManyToOne(targetEntity="Address", cascade={"all"}, fetch="EAGER")
* @ORM\JoinColumn(name="pick_up_address_id", referencedColumnName="id")
*/
protected $pickUpAddress;
/**
* @Assert\Valid()
* @ORM\ManyToOne(targetEntity="Address", cascade={"all"}, fetch="EAGER")
* @ORM\JoinColumn(name="drop_off_address_id", referencedColumnName="id")
*/
protected $dropOffAddress;
我在Adress
课程中添加了这些注释:
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="Reservation",
* mappedBy="pickUpAddress",
* cascade={"all"})
*/
private $pickUpReservations;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="Reservation",
* mappedBy="dropOffAddress",
* cascade={"all"})
*/
private $dropOffReservations;
我的表格:
->add(
'pickUpAddress',
EntityType::class,
array(
'class' => Address::class,
'property' => 'display',
'label' => 'pick_up_address',
'translation_domain' => 'front',
'empty_value' => 'new_address',
'query_builder' => function (AddressRepository $addressRepository) {
return $addressRepository->getAdressesByUser($this->user);
},
'required' => false
)
我希望在新的预订表单中显示为给定用户预先选择的所有地址的列表。
例如:如果我在我的家庭地址预订接送服务,而我的办公室地址则需要预订。对于我的下一个预订,我想要两个选择(html选择)与我的家庭地址和我的办公室地址为dropOffAdress和pickUpAdress两个字段。
我有这个SQL:
SELECT * FROM `address`
JOIN reservation
on reservation.pick_up_address_id = address.id
OR reservation.drop_off_address_id = address.id
我只需要添加where
子句以仅选择其中一个登录用户,但这不是问题。
我无法管理与查询构建器的连接。我尝试了很多组合,如:
$qb->select('pick_up_address', 'drop_off_adress')
->from(Reservation::class, 'reservations')
->join('reservations.pickUpAddress', 'pick_up_address')
->join('reservations.dropOffAddress', 'drop_off_adress')
$qb->select('Address')
->from(Address::class, 'Address')
->join('Address.pickUpReservations', 'pick_up_reservations')
->join('Address.dropOffReservations', 'drop_off_reservations')
但它不起作用。如果我使用DQL它确实有效(我的存储库给我结果)但表单构建器需要一个queryBuilder对象而不是Adress
答案 0 :(得分:0)
我清除了缓存,添加了注释反转:
/**
* @Assert\Valid()
* @ORM\ManyToOne(targetEntity="Address", cascade={"all"}, fetch="EAGER", inversedBy="pickUpReservations")
* @ORM\JoinColumn(name="pick_up_address_id", referencedColumnName="id")
*/
protected $pickUpAddress;
/**
* @Assert\Valid()
* @ORM\ManyToOne(targetEntity="Address", cascade={"all"}, fetch="EAGER", inversedBy="dropOffReservations")
* @ORM\JoinColumn(name="drop_off_address_id", referencedColumnName="id")
*/
protected $dropOffAddress;
并将最后一个转换为使用左连接:
return $qb->select('Address')
->leftjoin('Address.pickUpReservations', 'pickUpReservations')
->leftjoin('Address.dropOffReservations', 'dropOffReservations');
似乎有效