我正在使用 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
第三个实体 Spell 是一个简单的实体,有 id、name 和 description,没有映射,因为我试图避免不必要的数据库调用
这是控制器
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.
因此,如果我理解该消息,则学说无法保留整个关联,因为他试图首先保留 SpellListDetails 而不是 SpellList 并抛出异常,因为 SpellList 没有 id。我认为这可以通过 Entity 中的 mappedBy 和 persist 选项来避免,所以我做错了什么?
之前有什么方法可以手动调用 SpellList 实体上的持久化操作吗?
谢谢。