Symfony 3表单集合不持久外键

时间:2017-04-03 07:19:14

标签: php symfony doctrine-orm symfony-forms symfony-3.1

我嵌入了一系列表格。当我添加新记录时,外键始终为空。

有人知道问题在哪里吗?我试图循环结果并保留每条记录,但外键仍为空。

控制器:

 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(),
        ];
    }

巴士实体(父母)

/** @ORM\OneToMany(
     *     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 \AppBundle\Entity\BusVehiclesAmenities 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 \AppBundle\Entity\BusVehiclesAmenities $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 \AppBundle\Entity\BusVehiclesAmenities $busVehiclesAmenities
     * @return ArrayCollection
     */
   public function setBusVehiclesAmenities($busVehiclesAmenities)
    {
        $this->busVehiclesAmenities = $busVehiclesAmenities;

    }

巴士设施实体(儿童):

/**
     *
     * @ORM\ManyToOne(targetEntity="\AppBundle\Entity\BusVehicles", inversedBy="busVehiclesAmenities")
     * @ORM\JoinColumn(name="bus_vehicles_id", referencedColumnName="id")
     */
    private $busVehicles;

    /**
     *
     * @ORM\ManyToOne(targetEntity="\AppBundle\Entity\Amenities", inversedBy="amenities")
     * @ORM\JoinColumn(name="amenities_id", referencedColumnName="id")

     *
     */
    private $amenities;

    /**
     * @var float
     * @ORM\Column(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 \AppBundle\Entity\BusVehicles
     *
     * @return BusVehiclesAmenities
     */
    public function addBusVehicles(BusVehicles $busVehicles)
    {

        $this->busVehicles = $busVehicles;

        //$bus->setBusVehiclesAmenities($this);


        return $this;
    }

    /**
     * Remove bus
     *
     * @param \AppBundle\Entity\BusVehicles
     *
     * @return BusVehiclesAmenities
     */
    public function removeBusVehicles(BusVehicles $busVehicles = null)
    {

        $this->busVehicles = null;

        //$bus->setBusVehiclesAmenities($this);


        return $this;
    }

1 个答案:

答案 0 :(得分:0)

您将BusVehicle和Amenity映射为双向关联。 这些设施的fk为null,因为您没有为设施设置BusVehicle。

你需要实现一个方法,比如:在settings类中的setBusVehicle():

public function setBusVehicle(BusVehicles $bus) {
    $this->busVehicles = $bus;
}

使用双向映射时,必须从两侧设置对象。