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

问题描述

| 我正在启动我的第一个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\",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\",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\",length=4)
 * @Id
 * @GeneratedValue(strategy=\"IDENTITY\")
 */
private $ent4Code;

/**
 * @var string $att1    
 * @Column(name=\"att1\",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有关的重要信息,以及在刷新它们时它们如何工作。 我是否缺少一些重要的东西?     

解决方法

您的关系都是ManyToOne,因此不应该涉及ArrayCollections。 由于没有收藏,所以您不想
add
的东西,而您想要
set
的东西: 在Entity1中:
public function setEntity2(Entity2 $entity2){
    $this->entity2 = $entity2
    return $this;
}
在您的控制器中:
$entity1->setEntity2($entity2);
就是这样。像$ this-> entity2-> add()之类的调用正在工作,因为您正在将这些属性初始化为ArrayCollections。但是学说只是无视它们。 换句话说,对于* ToOne关系,对象属性只是外部实体类型。将它们视为简单值,然后通过典型的set *()突变器进行设置。