Doctrine仅保存最后一个集合

时间:2014-10-21 15:38:37

标签: php doctrine-orm zend-framework2 doctrine fieldset

我需要一些帮助,我在学说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;
            }
}

塔巴尔

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

}

FieldSet中 TaFoo Fieldset

*[..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。

1 个答案:

答案 0 :(得分:0)

您需要在实体类__construct方法中initialize all your collections

我认为问题在于那里,因为我在$this->idATypeBars实体中看不到TaBar的初始化。

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

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

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