1

Doctrine2 を使用して初めての Zend Framework アプリケーションを開発していますが、作業を単純な関連付けにすることができません。編集はうまくいきますが、エンティティの作成がうまくいきません。

私のエンティティは次のとおりです。

<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class SpellListDetail{


     /** 
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Application\Entity\SpellList", inversedBy="spellDetails") 
     * @ORM\JoinColumn(name="SpellList_id", referencedColumnName="id", nullable=false) 
     */
    private $sList;
    /** 
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Application\Entity\Spell") 
     * @ORM\JoinColumn(name="Spell_id", referencedColumnName="id", nullable=false) 
     */
    private $spell;

    /** @ORM\Column(type="integer") */
    private $level;

    /** @ORM\Column(type="float") */
    private $areaValueOne;
    /** @ORM\Column(type="float") */
    private $areaValueTwo;
    /** @ORM\Column(type="float") */
    private $areaValueThree;
    /** @ORM\ManyToOne(targetEntity="Application\Entity\AreaType") */
    private $areaType;
    /** @ORM\Column(type="boolean") */
    private $areaLevel = false;

    /** @ORM\Column(type="integer") */
    private $durationValue;
    /** @ORM\Column(type="integer") */
    private $durationType;
    /** @ORM\Column(type="integer") */
    private $durationFail;           

    /** @ORM\Column(type="float") */
    private $rangeValue = 0;

    /** @ORM\Column(type="integer") */
    private $rangeType;

    /** @ORM\Column(length=2) */
    private $primaryClass;

    /** @ORM\Column(length=1) */
    private $subClass;

    /** @ORM\Column(type="boolean") */
    private $instantaneous = false;

    /** @ORM\Column(type="boolean") */
    private $points = true;

//Setters & getter

<?php

namespace Application\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity (repositoryClass="Application\Repositories\SpellList") 
 */
class SpellList{
    const OPEN_LIST = 1000;
    const CLOSED_LIST = 2000;
    const EVIL_LIST = 3000;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(length=40)
     */
    private $name;

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
     * @ORM\ManyToOne(targetEntity="Application\Entity\Realm")
     */
    private $realm;

    /**
     * @ORM\Column(type="integer")
     */
    private $type;

     /** @ORM\OneToMany(targetEntity="Application\Entity\SpellListDetail", mappedBy="sList", cascade={"persist", "remove"}) 
      *  @ORM\OrderBy({"level" = "ASC"})*/
    private $spellDetails;
//Setters & getters

3 番目のエンティティである Spell は、ID、名前、および説明を持つ単純なエンティティであり、不要なデータベース呼び出しを回避しようとしているため、マッピングはありません。

そしてこれがコントローラー

public function createAction()
    {
        $form = new \Magic\Form\ListForm($this->model->getEntityManager());
        $form->setAttribute('action', '/magic/list/create');

        $spellList = new \Application\Entity\SpellList();
        $form->bind($spellList);

        if ($this->getRequest()->isPost()) {
            $form->setData($this->getRequest()->getPost());         
            if ($form->isValid()){
                foreach($spellList->getSpellDetails() as $detail){
                    $spell = $detail->getSpell();
                    if ($spell->getId() == null){
                        $this->model->getEntityManager()->persist($spell);
                        $this->model->getEntityManager()->flush();
                    }
                }                   
                $this->model->getEntityManager()->persist($spellList);
                //$this->model->getEntityManager()->flush();    
                $this->model->getEntityManager()->flush();
            }           
        }       
        $viewModel = new ViewModel(array('form' => $form));
        $viewModel->setTemplate('magic/list/edit.phtml');   
        return $viewModel;
    }

次のエラーメッセージが表示されます

Entity of type Application\Entity\SpellListDetail has identity through a foreign entity Application\Entity\SpellList, however this entity has no identity itself. You have to call EntityManager#persist() on the related entity and make sure that an identifier was generated before trying to persist 'Application\Entity\SpellListDetail'. In case of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call EntityManager#flush() between both persist operations.

したがって、私がメッセージを理解していれば、SpellList よりも SpellListDetails を最初に永続化しようとしており、SpellList には ID がないため例外をスローするため、教義は関連付け全体を永続化できません。これは、EntityのmappedByおよびpersistオプションで回避できると思ったので、何が間違っているのでしょうか?

前に SpellList エンティティで永続化操作を手動で呼び出す方法はありますか?

ありがとう。

4

1 に答える 1