Это мой первый проект Symfony, и я не могу понять, как решить мою проблему.
В основном, я пытаюсь сделать счет-фактуру, используя форму.
У меня есть "Facturation" (т.е. счет-фактура) Entity, Entity "TypeOfService" и "Service" Entity (единственным атрибутом которого является количество типа услуги, необходимой для счета-фактуры), которая действует как класс ассоциации.
Я хотел бы динамически добавлять поля "Новая услуга" в мою форму FacturationType с помощью Javascript (возможно, AngularJS). Поэтому мне нужно создать 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;
}
}
Может кто-то указать мне верное направление?
Для тех, кто смотрит, я взломал его.
Это то, что я искал:
http://symfony.com/doc/current/cookbook/form/form_collections.html
Это позволяет добавлять динамические поля, используя JS. Будьте осторожны, чтобы каскадировать вашу связь. В моем случае:
class Facturation
{
....
/**
* @ORM\OneToMany(targetEntity="Service", mappedBy="facturation", cascade={"persist", "remove"})
*/
private $services;