Zend + Doctrine2:如何使用ArrayCollections()正确刷新实体?

时间:2011-04-14 13:14:32

标签: zend-framework orm doctrine doctrine-orm

我正在开始我的第一个Zend Framework + Doctrine 2项目,我有一个问题。我使用PostgreSQL 9和Apache 2.2 我有以下实体(实体和属性的名称仅用于此示例):

<?php
namespace Xproject\Entities;
/**
 * Entity1   
 * @Table()  
 * @Entity  
 */      
class Entity1
{  
/***  
 * @var integer $ent1Code     
 * @Column(name="ent1Code", type="integer", length=4)  
 * @Id  
 * @GeneratedValue(strategy="IDENTITY")  
 */  
private $ent1Code;

/**
 * @var decimal $att1     
 * @Column(name="att1", type="decimal")
 */
private $att1;

 /** 
 * OWNING SIDE
 * @var \Doctrine\Common\Collections\ArrayCollection     
 * @ManyToOne(targetEntity="Entity2", inversedBy="entity1")
 * @JoinColumn(name="ent2Code", referencedColumnName="ent2Code")
 */
private $entity2;

/** 
 * UNIDIRECTIONAL
 * @var \Doctrine\Common\Collections\ArrayCollection
 * @ManyToOne(targetEntity="Entity3")
 * @JoinColumn(name="ent3Code", referencedColumnName="ent3Code")
 */
private $entity3;

 /**
 * UNIDIRECTIONAL
 * @var \Doctrine\Common\Collections\ArrayCollection
 * @ManyToOne(targetEntity="Entity4")
 * @JoinColumn(name="ent4Code", referencedColumnName="ent4Code")
 */
private $entity4;

public function __construct() {
    $this->entity2 = new \Doctrine\Common\Collections\ArrayCollection();
    $this->entity3 = new \Doctrine\Common\Collections\ArrayCollection();
    $this->entity4 = new \Doctrine\Common\Collections\ArrayCollection();
}

public function getEnt1Code(){
    return $this->ent1Code;
}

public function getAtt1(){
    return $this->att1;
}

public function setAtt1($value){
    $this->att1=$value;
}

public function addEntity2(Entity2 $value){
    $value->addEntity1($this);
    $this->entity2->add($value);
}

public function addEntity3(Entity3 $value){
    $this->entity3->add($value);
}

public function addEntity4(Entity4 $value){
    $this->entity4->add($value);
  }  
}

<?php
namespace Xproject\Entities;
/**
 * Entity2 
 * @Table()
 * @Entity
 */
class Entity2
 {
/**
 * @var integer $ent2Code     
 * @Column(name="ent2Code", type="integer", length=4)
 * @Id
 * @GeneratedValue(strategy="IDENTITY")
 */
private $ent2Code;

/**
 * INVERSE SIDE
 * @var entity1    
 * @OneToMany(targetEntity="Entity1", mappedBy="entity2")
 */
private $entity1;

public function __construct() {
    $this->entity1 = new \Doctrine\Common\Collections\ArrayCollection();
}

public function getEnt2Code(){
    return $this->ent2Code;
}

public function addEntity1(Entity1 $value){
    $this->entity1->add($value);
}
}

<?php
namespace Xproject\Entities;

/**
 * Entity3
 * @Table()
 * @Entity
 */
class Entity3
{
/**
 * @var integer $ent3Code     
 * @Column(name="ent3Code", type="integer", length=4)
 * @Id
 * @GeneratedValue(strategy="IDENTITY")
 */
private $ent3Code;

/**
 * @var string $att1     
 * @Column(name="att1", type="string", length=150)
 */
private $att1;

public function getEnt3Code(){
    return $this->ent3Code;
}

public function getAtt1(){
    return $this->att1;
}

public function setAtt1($value){
    $this->att1=$value;
}

}

<?php   
namespace Xproject\Entities;
/**
 * Entity4 
 * @Table()
 * @Entity
 */
 class Entity4 
{
/**
 * @var integer $ent4Code   
 * @Column(name="ent4Code", type="integer", length=4)
 * @Id
 * @GeneratedValue(strategy="IDENTITY")
 */
private $ent4Code;

/**
 * @var string $att1    
 * @Column(name="att1", type="string", length=150)
 */
private $att1;

public function getEnt4Code(){
    return $this->ent4Code;
}

public function getAtt1(){
    return $this->att1;
}

public function setAtt1($value){
    $this->att1=$value;
}
}

只是尝试一切正常我在Xproject的indexController中使用以下代码:

<?php
class IndexController extends Zend_Controller_Action
{
    public function init()
   {
        $this->doctrine = Zend_Registry::get('doctrine');
        $this->em = $this->doctrine->getEntityManager();
    }

public function indexAction()
{
    $ent2 = new Xproject\Entities\Entity2();
    $this->em->persist($ent2);

    $ent3 = new Xproject\Entities\Entity3();
    $ent3->setAtt1('xyz');
    $this->em->persist($ent3);

    $ent4= new Xproject\Entities\Entity4();
    $ent4->setAtt1('abc');
    $this->em->persist($ent4);

    //1st flush
    $this->em->flush();                       

    $ent1= new Xproject\Entities\Entity1();
    $ent1->setAtt1(350.00);
    $ent1->addEntity2(ent2);
    $ent1->addEntity3(ent3);
    $ent1->addEntity4(ent4);
    $this->em->persist($ent1);

    //2nd flush
    //$this->em->flush();     
}
}

第一次刷新工作正常,所有内容都保存到数据库中,但如果我同时使用第一次和第二次刷新,则浏览器会指示应用程序错误,并且$ ent1根本不会保存到数据库中。
使用var_dump($ ent1)我可以看到对象$ ent1状态是正确的(att1和所有集合都加载好了)。
在加载此脚本期间,Apache错误日志不会显示任何错误或警告 我绝对认为我遗漏了一些与ArrayCollections有关的重要事项以及它们在冲洗它们时的工作原理 我错过了一些关键的东西吗

1 个答案:

答案 0 :(得分:1)

您的关系都是ManyToOne,因此不应该涉及ArrayCollections。

由于没有收藏品,您不想要add内容,您想要set内容:

在Entity1中:

public function setEntity2(Entity2 $entity2){
    $this->entity2 = $entity2
    return $this;
}

在您的控制器中:

$entity1->setEntity2($entity2);

就是这样。您的调用如$ this-&gt; entity2-&gt; add()正在运行,因为您正在将这些属性初始化为ArrayCollections。但是,学说只是忽略了它们。

换句话说,对于* ToOne关系,对象属性只是外来实体类型。像简单的值一样对待它们,并通过典型的set *()mutator设置它们。