2011-03-16 49 views
5

我有一个ZendFramework 1.11.2中使用Doctrine 2作为ORM的双向OneToMany关系。doctrine2 OneToMany关系插入NULL作为外键

注意:Doctrine没有创建数据库表。数据库是MySQL。

由于某些原因,当我坚持并刷新链接实体到链接表(见下文)时,外键字段(container_id)被设置为NULL。但是,如果从'ManyToOne(targetEntity =“Shepherd \ Navigation \ Domain \ Container \ Model”,inversedBy =“links”)'行中删除'@'符号,则会正确填充外键字段。

由于实体在删除'@'符号时已正确添加到数据库,因此OneToMany关系在某处出现问题。

举例来说,如果我有一个名为$链路的链路模型(见下面的伪代码)...

$link (Shepherd\Navigation\Domain\Link\Model) 
    { 
     id: ''  // auto generated value 
     cid: 23  // the foreign key value 
     label: test 
     uri: test.com 
     ...   // other values not listed here for brevity 
    } 

...当新的链路模型被持久化和实体管理器被刷新,链接(shepherd_navigation_link)表中新插入的行的container_id(外键)值为NULL。

$em // Assume $em is the Entity Manager 
    $em->persist($link); 
    $em->flush(); 

    // The container_id in the newly added row in the 
    // link table (shepherd_navigation_link) is NULL 

链接表模式:

CREATE TABLE `shepherd_navigation_link` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `container_id` int(10) unsigned DEFAULT NULL, 
    `node_id` int(10) unsigned DEFAULT NULL, 
    `parent_id` int(10) unsigned DEFAULT NULL, 
    `label` varchar(100) NOT NULL, 
    `options` text, 
    `events` text, 
    `privilege` varchar(100) NOT NULL, 
    `resource` varchar(100) DEFAULT NULL, 
    `uri` varchar(300) NOT NULL, 
    `visible` int(10) unsigned DEFAULT '1', 
    PRIMARY KEY (`id`), 
    KEY `container_id` (`container_id`) 
) ENGINE=InnoDB 
ALTER TABLE `shepherd_navigation_link` ADD FOREIGN KEY (container_id) REFERENCES shepherd_navigation_container(id) 

链路实体模型:

/** 
* @Entity 
* @Table(name="shepherd_navigation_link") 
*/ 
class 
{ 
    /** 
    * @Id 
    * @Column(type="integer") 
    * @GeneratedValue 
    */ 
    protected $id; 

    /** 
    * @Column(name="container_id", type="integer", nullable=false) 
    */ 
    protected $cid; 

    /** 
    * @Column(name="node_id", type="integer") 
    */ 
    protected $nid; 

    /** 
    * @Column(name="parent_id", type="integer", nullable=false) 
    */ 
    protected $pid; 

    /** 
    * @Column 
    */ 
    protected $label; 

    /** 
    * @Column(nullable=true) 
    */ 
    protected $options; 

    /** 
    * @Column(nullable=true) 
    */ 
    protected $events; 

    /** 
    * @Column 
    */ 
    protected $privilege; 

    /** 
    * @Column(nullable=true) 
    */ 
    protected $resource; 

    /** 
    * @Column 
    */ 
    protected $uri; 

    /** 
    * @Column(type="integer", nullable=true) 
    */ 
    protected $visible; 

    /** 
    * @OneToMany(targetEntity="Model", mappedBy="parent") 
    */ 
    private $children; 

    /** 
    * @ManyToOne(targetEntity="Model", inversedBy="children") 
    */ 
    private $parent; 

    /** 
    *) @ManyToOne(targetEntity="Shepherd\Navigation\Domain\Container\Model", inversedBy="links" 
    */ 
    private $container; 

    /** 
    * @OneToOne(targetEntity="Shepherd\Navigation\Domain\Link\Position", inversedBy="link") 
    */ 
    private $node; 

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

    /** Accessors and Mutators excluded for brevity **/ 
} 

注:受保护的属性$ CID映射到以上CONTAINER_ID列。

容器表模式:

CREATE TABLE `shepherd_navigation_container` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `name` varchar(100) NOT NULL, 
    `description` text, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB 

容器实体模型:

/** 
* @Entity 
* @Table(name="shepherd_navigation_container") 
*/ 
class Model 
{ 
    /** 
    * @Id 
    * @Column(type="integer") 
    * @GeneratedValue 
    */ 
    protected $id; 

    /** 
    * @Column 
    */ 
    protected $name; 

    /** 
    * @Column(nullable=true) 
    */ 
    protected $description; 

    /** 
    * @OneToMany(targetEntity="Shepherd\Navigation\Domain\Link\Model", mappedBy="container") 
    */ 
    private $links; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->links = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** Accessors and Mutators excluded for brevity **/ 
} 

我缺少什么?我究竟做错了什么?

+0

我不相信你需要在你的关联映射中的目标实体的整个路径。它看起来像一些你有整个路径,其他人只有实体名称。我不确定这是否是造成问题的原因,但这是你可以看到的。 – 2011-03-18 15:19:45

+0

谢谢杰里米,你是对的。我在命名模型时不一致。但问题仍然存在。 – user175590 2011-03-22 18:51:10

回答

5

我想通了(通过阅读文档http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/getting-started-xml-edition.html)。事实证明,实际上存在一些问题。

问题1 =>我没有提供设置容器变量的方法。

// Inside the Link Entity class... 

public function setContainer($container) 
{ 
    $this->container = $container; 
} 

问题2 =>我没有设置容器值。错误的是,我认为Doctrine 2在内部做到了这一点,但是我发现在冲洗之前需要设置容器变量。

对我而言,这是愚蠢的疏忽。

$link = new Link(); 
$link->setContainer($container); 

// $em is the Entity Manager 
$em->persist($link); 
$em->flush(); 

问题3 =>的容器($容器)需要要么之前冲洗或在需要改变容器实体@OneToMany定义持续存在。我选择更新容器实体定义。看看这里(http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-associations.html#transitive-persistence-cascade-operations)了解更多信息。

// Inside the Container Entity class... 
/** 
* @OneToMany(targetEntity="Shepherd\Navigation\Domain\Link\Model", mappedBy="container", cascade={"persist"}) 
*/ 

做出这些改变和删除的链接实体类(原来我并不需要它)的@OneToOne节点的关系后,一切正常。我希望这可以帮助别人。