条令只保存最后的收藏品


Doctrine saves only last collection

我需要一些帮助,我用条令2研究ZF2。我有一个实体:带孩子的TaFoo:TaBar(ArrayCollection)。TaBar也有子类:ATypeBar(ArrayCollection)关联到TypeBar。我使用FieldSets添加多个TaBar,并在每个TaBar上添加多个ATypeBars。

但当我保存时:

  • TaFoo是创建和保存
  • TaBar也在创建和保存
  • TaTypeBar只保存最后一个集合

我没有任何错误,但集合下的第一个永远不会保存

实体: TaFoo

  class TaFoo{
        /**
         * @var integer
         * @ORM'Column(name="ID_FOO", type="integer", nullable=false)
         * @ORM'Id
         * @ORM'GeneratedValue(strategy="SEQUENCE")
         * @ORM'SequenceGenerator(sequenceName="TA_FOO_ID_FOO_seq", allocationSize=1, initialValue=1)
         */
        private $idFoo;
        /**
         * Get idFoo
         *
         * @return integer
         */
        public function getIdFoo()
        {
            return $this->idFoo;
        }
        /**
        * @ORM'OneToMany(targetEntity="TaBar", mappedBy="idFoo", cascade={"persist", "remove"}, orphanRemoval=true)
         **/
        private $idBar;
        /**
         * Get idBar
         *
         * @return 'Doctrine'Common'Collections'Collection
         */
        public function getIdBar()
        {
            return $this->idBar;
        }
         /**
         * Remove idBar
         *
         * @param 'Doctrine'Common'Collections'Collection $idBar
         */
        public function removeIdBar('Doctrine'Common'Collections'Collection $idBars)
        {
            foreach ($idBars as $idBar) {
                $idBar->setIdFoo(null);
                $this->idBar->removeElement($idBar);
            }
        }
         /**
         * Add idBar
         *
         * @param 'Doctrine'Common'Collections'Collection $idBar
         * @return TaFoo
         */
        public function addIdBar('Doctrine'Common'Collections'Collection $idBars)
        {
            foreach ($idBars as $idBar) {
                $idBar->setIdFoo($this);
                $this->idBar->add($idBar);
            }
            return $this;
            }
}

TaBar

class TaBar {
// **************************************************TYPE BAR***************************************************
/**
 * @ORM'OneToMany(targetEntity="Application'Entity'ATypeBar", mappedBy="idBar", cascade={"persist", "remove"}, orphanRemoval=true)
 * */
private $idATypeBars;
/**
 * Get typeBar
 * @return 'Doctrine'Common'Collections'Collection
 */
public function getIdATypeBars() {
    return $this->idATypeBars;
}
/**
 * Remove typeBar
 * @param 'Doctrine'Common'Collections'Collection $typeBars
 */
public function removeIdATypeBars('Doctrine'Common'Collections'Collection $idATypeBars) {
    foreach ($idATypeBars as $idATypeBar) {
        $idATypeBar->setIdBar(null);
        $this->idATypeBars->removeElement($idATypeBar);
    }
}
/**
 * Add typeBar
 * @param 'Doctrine'Common'Collections'Collection $typeBars
 * @return TaBar
 */
public function addIdATypeBars('Doctrine'Common'Collections'Collection $idATypeBars) {
    foreach ($idATypeBars as $idATypeBar) {
        $idATypeBar->setIdBar($this);
        $this->idATypeBars->add($idATypeBar);
    }
    return $this;
}

ATypeBar

class ATypeBar
{
    /**
     * @var integer
     *
     * @ORM'Column(name="ID_A_TYPE_Bar", type="integer", nullable=false)
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="SEQUENCE")
     * @ORM'SequenceGenerator(sequenceName="A_TYPE_Bar_ID_A_TYPE_Bar_seq", allocationSize=1, initialValue=1)
     */
    private $idATypeBar;
    /**
     * @var 'Application'Entity'TaBar
     *
     * @ORM'ManyToOne(targetEntity="Application'Entity'TaBar",cascade={"persist"},inversedBy="idATypeBar")
     * @ORM'JoinColumns({
     *   @ORM'JoinColumn(name="ID_Bar", referencedColumnName="ID_Bar")
     * })
     */
    private $idBar;
    /**
     * @var 'Application'Entity'TaTypeBar
     *
     * @ORM'ManyToOne(targetEntity="Application'Entity'TaTypeBar")
     * @ORM'JoinColumns({
     *   @ORM'JoinColumn(name="ID_TYPE_Bar", referencedColumnName="ID_TYPE_Bar")
     * })
     */
    private $idTypeBar;

    /**
     * Get idATypeBar
     *
     * @return integer 
     */
    public function getIdATypeBar()
    {
        return $this->idATypeBar;
    }
    /**
     * Set idBar
     *
     * @param 'Application'Entity'TaBar $idBar
     * @return ATypeBar
*/
    public function setIdBar('Application'Entity'TaBar $idBar = null)
    {
        $this->idBar = $idBar;
        return $this;
    }
    /**
     * Get idBar
     *
     * @return 'Application'Entity'TaBar
     */
    public function getIdBar()
    {
        return $this->idBar;
    }
    /**
     * Set idTypeBar
     *
     * @param 'Application'Entity'TaTypeBar $idTypeBar
     * @return ATypeBar
     */
    public function setIdTypeBar('Application'Entity'TaTypeBar $idTypeBar = null)
    {
        $this->idTypeBar = $idTypeBar;
        return $this;
    }
    /**
     * Get idTypeBar
     *
     * @return 'Application'Entity'TaTypeBar
     */
    public function getIdTypeBaru()
    {
        return $this->idTypeBar;
    }
}

TaTypeBar

class TaTypeBar {
    /**
     * @ORM'OneToMany(targetEntity="ATypeBar", mappedBy="idTypeBar", cascade={"persist", "remove"}, orphanRemoval=true)
     * */
    private $typeBar;

    /**
     * @var integer
     *
     * @ORM'Column(name="ID_TYPE_Bar", type="integer", nullable=false)
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="SEQUENCE")
     * @ORM'SequenceGenerator(sequenceName="TA_TYPE_BarDE_ID_TYPE_Bar_seq", allocationSize=1, initialValue=1)
     */
    private $idTypeBar;
    /**
     * Constructor
     */
    public function __construct() {
        $this->idBar = new 'Doctrine'Common'Collections'ArrayCollection();
    }
    /**
     * Get idTypeBar
     *
     * @return integer 
     */
    public function getIdTypeBar() {
        return $this->idTypeBar;
    }
    /**
     * Add idBar
     *
     * @param 'Application'Entity'TaBar $idBar
     * @return TaTypeBar
     */
    public function addIdBar('Application'Entity'TaBar $idBar) {
        $this->idBar[] = $idBar;
        return $this;
    }
}

字段集TaFoo字段集

*[..HERE: Some Elements, they work..]*
//****************************************************************************
// TAB BAR
//****************************************************************************
$this->add(array(
    'type' => 'Zend'Form'Element'Collection',
    'name' => 'idBar',
    'options' => array(
        // 'tab' => 'multiple',
        'label' => 'add a bar',
        'count' => 0,
        'allow_remove' => true,
        'allow_add' => true,
        'target_element' => new BarFieldset($entityManager, $userNumeroOrga),
    )
));
$this->get('idBar')->setHydrator(new DoctrineHydratorDateTimeFr($entityManager));

BarFieldset

*[..HERE: Some Elements, they work..]*
$this->add(array(
    'type' => 'Zend'Form'Element'Collection',
    'name' => 'idATypeBars',
    'options' => array(
        'tab' => 'Bar',
        'label' => 'add a TypeBar',
        'count' => 1,
        'allow_remove' => true,
        'allow_add' => true,
        'target_element' => new TypeBarFieldset($entityManager),
    )
));

控制器FooController

    $request = $this->getRequest();
    $idFoo = (int) $this->params()->fromRoute('id', 0);
    if ($request->isPost()) {
        $idFoo = $this->params()->fromPost('idFoo');
    }
    $taFoo = $this->getEntityManager()->find(''Application'Entity'TaFoo', $idFoo);
    $form = new FooForm($this->getEntityManager(), $this->getUserNum());
    $form->bind($taFoo);
    if ($request->isPost()) {
        $post = $request->getPost();
        $config = $this->getServiceLocator()->get('config');
        $fieldset = $config['fieldset'];
        $form->setData($post);
        if ($form->isValid()) {
            $this->getEntityManager()->persist($taFoo);
            $this->getEntityManager()->flush();
            echo ('ok');
            exit();
        }
        echo '-1: ';
        print_r($form->getMessages());
        exit();

我已经在$request->getPost中检查了数据是否正确,没有问题。保存TaFoo/TaBar/AtypeBar/TaTypeBar的所有数据都在中。但持久化不适用于TaTypeBar。

您需要在实体类__construct方法中初始化所有集合。

我认为问题就在那里,因为我在TaBar实体中没有看到任何$this->idATypeBars的初始化。

所以添加一个__construct函数,如下所示:

public function __construct(){
    $this->idATypeBars = new ArrayCollection();
}

还要检查其他实体的构造函数。这肯定会引起问题。