删除元素后保存集合


Save collection after removing elements

我有3个学说实体:

    产品
  • ProductStore

我需要有一个表单,允许使用Zend表单集合管理它们之间的关联。

添加结束编辑工作良好,但删除不保存在数据库中。换句话说,从集合中删除了所需的元素,但它没有反映在数据库中。

这是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;
    }
}

and ProductStore entity:

/**
 * 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;
    }
}

所以,问题是被删除的元素仍然在数据库中,尽管它们在持久化之前从集合中删除了。

这就是我需要的:http://docs.doctrine-project.org/en/latest/reference/working-with-associations.html orphan-removal

问题解决