【问题标题】:Symfony2 Association class formSymfony2 关联类形式
【发布时间】:2016-06-06 23:08:34
【问题描述】:

这是我的第一个 Symfony 项目,我不知道如何解决我的问题。

基本上,我正在尝试使用表格开具发票。

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

我想使用 Javascript(可能是 AngularJS)将“新服务”字段动态添加到我的 FacturationType 表单中。所以我必须创建 N 个新的 Service 实体,每个实体都与我的 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;
    }
}

谁能指出我正确的方向?

【问题讨论】:

    标签: angularjs forms symfony associations model-associations


    【解决方案1】:

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

    这就是我要找的东西:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多