Symfony2 Association类表单

时间:2016-06-06 23:08:34

标签: angularjs forms symfony associations model-associations

这是我的第一个Symfony项目,我无法弄清楚如何解决我的问题。

基本上,我正在尝试使用表单制作发票。

我有一个“Facturation”(即发票)实体,一个“TypeOfService”实体和一个“服务”实体(其唯一属性是发票所需的服务类型的数量),它充当了协会班。

我想使用Javascript(可能是AngularJS)将“新服务”字段动态添加到我的FacturationType表单中。所以我必须创建N个新的服务实体,每个实体都与我的Facturation实体和现有的TypeOfService实体相关联。

这是我的Facturation实体:     

use Doctrine\ORM\Mapping AS ORM;

/**
 * Facturation
 *
 * @ORM\Table(name="facturation")
 * @ORM\Entity
 */
class Facturation
{
    ...


    /**
     * @ORM\OneToMany(targetEntity="Service", mappedBy="facturation")
     */
    private $service;

    /**
     * Add service
     *
     * @param \AppBundle\Entity\Service $service
     * @return Facturation
     */
    public function addService(\AppBundle\Entity\Service $service)
    {
        $this->service[] = $service;

        return $this;
    }

    /**
     * Remove service
     *
     * @param \AppBundle\Entity\Service $service
     */
    public function removeService(\AppBundle\Entity\Service $service)
    {
        $this->service->removeElement($service);
    }

    /**
     * Get service
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getService()
    {
        return $this->service;
    }

}

然后服务:

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping AS ORM;

/**
 * Service
 *
 * @ORM\Table(name="service")
 * @ORM\Entity
 */
class Service
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $quantity;

    /**
     * @ORM\ManyToOne(targetEntity="TypeOfService", inversedBy="service")
     * @ORM\JoinColumn(name="type_of_service_id", referencedColumnName="id")
     */
    private $typeOfService;

    /**
     * @ORM\ManyToOne(targetEntity="Facturation", inversedBy="service")
     * @ORM\JoinColumn(name="facturation_id", referencedColumnName="id")
     */
    private $facturation;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set quantity
     *
     * @param integer $quantity
     * @return Service
     */
    public function setQuantity($quantity)
    {
        $this->quantity = $quantity;

        return $this;
    }

    /**
     * Get quantity
     *
     * @return integer 
     */
    public function getQuantity()
    {
        return $this->quantity;
    }

    /**
     * Set typeOfService
     *
     * @param \AppBundle\Entity\TypeOfService $typeOfService
     * @return Service
     */
    public function setTypeOfService(\AppBundle\Entity\TypeOfService $typeOfService = null)
    {
        $this->typeOfService = $typeOfService;

        return $this;
    }

    /**
     * Get typeOfService
     *
     * @return \AppBundle\Entity\TypeOfService 
     */
    public function getTypeOfService()
    {
        return $this->typeOfService;
    }

    /**
     * Set facturation
     *
     * @param \AppBundle\Entity\Facturation $facturation
     * @return Service
     */
    public function setFacturation(\AppBundle\Entity\Facturation $facturation = null)
    {
        $this->facturation = $facturation;

        return $this;
    }

    /**
     * Get facturation
     *
     * @return \AppBundle\Entity\Facturation 
     */
    public function getFacturation()
    {
        return $this->facturation;
    }
}

最后是TypeOfService

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping AS ORM;


/**
 * TypeOfService
 *
 * @ORM\Table(name="type_of_service")
 * @ORM\Entity
 */
class TypeOfService
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(nullable=true)
     */
    private $name;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $pricePerUnit;

    /**
     * @ORM\OneToMany(targetEntity="Service", mappedBy="typeOfService")
     */
    private $service;

    ...

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->service = new \Doctrine\Common\Collections\ArrayCollection();
    }


    /**
     * Add service
     *
     * @param \AppBundle\Entity\Service $service
     * @return TypeOfService
     */
    public function addService(\AppBundle\Entity\Service $service)
    {
        $this->service[] = $service;

        return $this;
    }

    /**
     * Remove service
     *
     * @param \AppBundle\Entity\Service $service
     */
    public function removeService(\AppBundle\Entity\Service $service)
    {
        $this->service->removeElement($service);
    }

    /**
     * Get service
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getService()
    {
        return $this->service;
    }
}

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

对于任何正在寻找的人,我已经破解了它。

这就是我想要的:

http://symfony.com/doc/current/cookbook/form/form_collections.html

这允许使用JS进行动态字段添加。 小心级联您的关联。就我而言:

class Facturation
{
....
    /**
     * @ORM\OneToMany(targetEntity="Service", mappedBy="facturation", cascade={"persist", "remove"})
     */
    private $services;