这是我的第一个Symfony项目,我不知道如何解决我的问题。
基本上,我正在尝试使用表格制作发票。
我有一个充当关联类的"制造"(即发票)实体、"服务类型"实体和"服务"实体(其唯一属性是发票所需服务类型的数量)。
我想使用 Javascript(可能是 AngularJS)将"新服务"字段动态添加到我的 FacturationType 表单中。因此,我必须创建 N 个新的服务实体,每个实体都与我的 Facturation 实体和现有的 TypeOfService 实体相关联。
这是我的事实实体:
use DoctrineORMMapping AS ORM;
/**
* Facturation
*
* @ORMTable(name="facturation")
* @ORMEntity
*/
class Facturation
{
...
/**
* @ORMOneToMany(targetEntity="Service", mappedBy="facturation")
*/
private $service;
/**
* Add service
*
* @param AppBundleEntityService $service
* @return Facturation
*/
public function addService(AppBundleEntityService $service)
{
$this->service[] = $service;
return $this;
}
/**
* Remove service
*
* @param AppBundleEntityService $service
*/
public function removeService(AppBundleEntityService $service)
{
$this->service->removeElement($service);
}
/**
* Get service
*
* @return DoctrineCommonCollectionsCollection
*/
public function getService()
{
return $this->service;
}
}
然后服务:
<?php
namespace AppBundleEntity;
use DoctrineORMMapping AS ORM;
/**
* Service
*
* @ORMTable(name="service")
* @ORMEntity
*/
class Service
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMColumn(type="integer", nullable=true)
*/
private $quantity;
/**
* @ORMManyToOne(targetEntity="TypeOfService", inversedBy="service")
* @ORMJoinColumn(name="type_of_service_id", referencedColumnName="id")
*/
private $typeOfService;
/**
* @ORMManyToOne(targetEntity="Facturation", inversedBy="service")
* @ORMJoinColumn(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 AppBundleEntityTypeOfService $typeOfService
* @return Service
*/
public function setTypeOfService(AppBundleEntityTypeOfService $typeOfService = null)
{
$this->typeOfService = $typeOfService;
return $this;
}
/**
* Get typeOfService
*
* @return AppBundleEntityTypeOfService
*/
public function getTypeOfService()
{
return $this->typeOfService;
}
/**
* Set facturation
*
* @param AppBundleEntityFacturation $facturation
* @return Service
*/
public function setFacturation(AppBundleEntityFacturation $facturation = null)
{
$this->facturation = $facturation;
return $this;
}
/**
* Get facturation
*
* @return AppBundleEntityFacturation
*/
public function getFacturation()
{
return $this->facturation;
}
}
最后是服务类型
<?php
namespace AppBundleEntity;
use DoctrineORMMapping AS ORM;
/**
* TypeOfService
*
* @ORMTable(name="type_of_service")
* @ORMEntity
*/
class TypeOfService
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMColumn(nullable=true)
*/
private $name;
/**
* @ORMColumn(type="integer", nullable=true)
*/
private $pricePerUnit;
/**
* @ORMOneToMany(targetEntity="Service", mappedBy="typeOfService")
*/
private $service;
...
/**
* Constructor
*/
public function __construct()
{
$this->service = new DoctrineCommonCollectionsArrayCollection();
}
/**
* Add service
*
* @param AppBundleEntityService $service
* @return TypeOfService
*/
public function addService(AppBundleEntityService $service)
{
$this->service[] = $service;
return $this;
}
/**
* Remove service
*
* @param AppBundleEntityService $service
*/
public function removeService(AppBundleEntityService $service)
{
$this->service->removeElement($service);
}
/**
* Get service
*
* @return DoctrineCommonCollectionsCollection
*/
public function getService()
{
return $this->service;
}
}
谁能指出我正确的方向?
对于任何看的人来说,我已经破解了它。
这就是我正在寻找的:
http://symfony.com/doc/current/cookbook/form/form_collections.html
这允许使用 JS 进行动态字段添加。请注意级联您的关联。就我而言:
class Facturation
{
....
/**
* @ORMOneToMany(targetEntity="Service", mappedBy="facturation", cascade={"persist", "remove"})
*/
private $services;