我嵌入了一个表单集合。当我添加新记录时,外键始终为空。
有人知道问题出在哪里吗?我试图循环结果并保留每条记录,但外键仍然是空的。
控制器:
public function newAction(Request $request)
{
$bus = new BusVehicles();
$busAmenities = new BusVehiclesAmenities();
// dump ($busAmenities);
// $this->getDoctrine()->getManager()->persist($busAmenities);
$bus->addBusVehiclesAmenities($busAmenities);
$form = $this->createForm(BusVehiclesType::class, $bus);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$form->get('busVehiclesAmenities')->getData()->map(
function ($amenities) use ($em, $bus) {
$bus->addBusVehiclesAmenities($amenities);
$em->persist($amenities);
}
);
// $em->persist($form->get('busVehiclesAmenities')->getData());
$em->persist($bus);
// Adding flash message to our user
$this->addFlash('success', 'admin.bus.created');
// return $this->redirectToRoute('bus_add');
}
return [
'form' => $form->createView(),
];
}
总线实体(父(
/** @ORMOneToMany(
* targetEntity="BusVehiclesAmenities",
* mappedBy="busVehicles",
* cascade={"persist"}
* )
*
*/
private $busVehiclesAmenities;
public function __construct()
{
$this->busVehiclesAmenities = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add busVehiclesAmenities
*
* @param AppBundleEntityBusVehiclesAmenities busVehiclesAmenities
* @return BusVehicles
*/
public function addBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
if ($this->busVehiclesAmenities->contains($busVehiclesAmenities)) {
return;
}
$this->busVehiclesAmenities->add($busVehiclesAmenities);
$busVehiclesAmenities->addBusVehicles($this);
return $this;
}
/**
* Remove busVehiclesAmenities
*
* @param AppBundleEntityBusVehiclesAmenities $busVehiclesAmenities
*/
public function removeBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
if (!$this->busVehiclesAmenities->contains($busVehiclesAmenities)) {
return;
}
$this->busVehiclesAmenities->removeElement($busVehiclesAmenities);
$busVehiclesAmenities->removeBusVehicles(this);
}
/**
* Get busVehiclesAmenities
*
* @return ArrayCollection
*/
public function getBusVehiclesAmenities()
{
return $this->busVehiclesAmenities;
}
/**
* Set busVehiclesAmenities
*
* @param AppBundleEntityBusVehiclesAmenities $busVehiclesAmenities
* @return ArrayCollection
*/
public function setBusVehiclesAmenities($busVehiclesAmenities)
{
$this->busVehiclesAmenities = $busVehiclesAmenities;
}
巴士设施实体(儿童(:
/**
*
* @ORMManyToOne(targetEntity="AppBundleEntityBusVehicles", inversedBy="busVehiclesAmenities")
* @ORMJoinColumn(name="bus_vehicles_id", referencedColumnName="id")
*/
private $busVehicles;
/**
*
* @ORMManyToOne(targetEntity="AppBundleEntityAmenities", inversedBy="amenities")
* @ORMJoinColumn(name="amenities_id", referencedColumnName="id")
*
*/
private $amenities;
/**
* @var float
* @ORMColumn(name="price", type="float", scale=2)
*/
protected $price;
public function __construct()
{
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get busId
*
* @return integer
*/
public function getBusVehicles()
{
return $this->busVehicles;
}
/**
* Add bus
*
* @param AppBundleEntityBusVehicles
*
* @return BusVehiclesAmenities
*/
public function addBusVehicles(BusVehicles $busVehicles)
{
$this->busVehicles = $busVehicles;
//$bus->setBusVehiclesAmenities($this);
return $this;
}
/**
* Remove bus
*
* @param AppBundleEntityBusVehicles
*
* @return BusVehiclesAmenities
*/
public function removeBusVehicles(BusVehicles $busVehicles = null)
{
$this->busVehicles = null;
//$bus->setBusVehiclesAmenities($this);
return $this;
}
您将巴士车辆和便利设施映射为双向关联。便利设施的 fk 为空,因为您没有为便利设施设置巴士车辆。
你需要实现一个方法,比如:在 Utilities 类中设置总线车辆((:
public function setBusVehicle(BusVehicles $bus) {
$this->busVehicles = $bus;
}
使用双向映射时,必须从两侧设置对象。