我有3个学说实体:
我需要一个允许使用Zend Form Collections管理它们之间关联的表单。
添加结束编辑效果很好,但删除不会保存在数据库中。换句话说,所需的元素正在从集合中删除,但它并没有反映在数据库中。
这是ProductsController:
<?php
class ProductsController extends AbstractActionController
{
public function linkAction()
{
$request = $this->getRequest();
$id = (int)$this->params()->fromRoute('id', null);
$em = $this->getEntityManager();
$formManager = $this->getServiceLocator()->get('FormElementManager');
$form = $formManager->get('ProductLinkForm');
$item = $em->find('Application\Entity\Product', $id);
$form->bind($item);
if ($this->request->isPost()) {
$form->setData($this->request->getPost());
if ($form->isValid()) {
$em->persist($item);
$em->flush();
return $this->redirect()->toRoute(...);
}
}
return new ViewModel(array(
'form' => $form,
'item' => $item,
));
}
}
字段集:
class ProductLinkFieldset extends Fieldset
{
protected $objectManager;
public function __construct(ObjectManager $objectManager)
{
parent::__construct('product_link_fieldset');
$this->objectManager = $objectManager;
}
public function init()
{
$this->setHydrator(new DoctrineHydrator($this->objectManager))
->setObject(new Product());
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'productStores',
'options' => array(
'count' => 0, // minimum elements count
'should_create_template' => true,
'allow_add' => true,
'allow_remove' => true,
'target_element' => array(
'type' => 'ProductStoreFieldset',
),
)
));
}
}
Product
实体是:
/**
* Product
*
* @ORM\Table(name="Product")
* @ORM\Entity
*/
class Product
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", precision=0, scale=0, nullable=false, unique=false)
*/
private $name;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Application\Entity\ProductStore", mappedBy="product", cascade={"persist"})
*/
private $productStores;
/**
* Constructor
*/
public function __construct()
{
$this->productStores = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add productStores
*
* @param Collection $productStores
*/
public function addProductStores(Collection $productStores)
{
foreach ($productStores as $productStore) {
$productStore->setProduct($this);
$this->productStores->add($productStore);
}
}
/**
* Remove productStores
*
* @param Collection $productStores
*/
public function removeProductStores(Collection $productStores)
{
foreach ($productStores as $productStore) {
# if I uncomment this, there will be an error when persisting the entity:
# An exception occurred while executing
# 'UPDATE product_store SET product_id = ? WHERE id = ?' with params [null, 4]:
# Column can not be null.
// $productStore->setProduct(null);
$this->productStores->removeElement($productStore);
}
return $this;
}
}
和ProductStore
实体:
/**
* ProductStore
*
* @ORM\Table(name="product_store")
* @ORM\Entity
*/
class ProductStore
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="url", type="string", precision=0, scale=0, nullable=false, unique=false)
*/
private $url;
/**
* @var \Application\Entity\Product
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Product", inversedBy="productStores", cascade={"persist","remove"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
* })
*/
private $product;
/**
* @var \Application\Entity\Store
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Store", inversedBy="storeProducts")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="store_id", referencedColumnName="id", nullable=false)
* })
*/
private $store;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set product
*
* @param \Application\Entity\Product $product
* @return ProductStore
*/
public function setProduct(\Application\Entity\Product $product = null)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return \Application\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
/**
* Set store
*
* @param \Application\Entity\Store $store
* @return ProductStore
*/
public function setStore(\Application\Entity\Store $store = null)
{
$this->store = $store;
return $this;
}
/**
* Get store
*
* @return \Application\Entity\Store
*/
public function getStore()
{
return $this->store;
}
}
所以,问题在于删除的元素仍然在数据库中,尽管它们在持久化之前已从集合中删除。
答案 0 :(得分:0)