Doctrine orm doctrine2 OneToMany关系将NULL插入为外键

Doctrine orm doctrine2 OneToMany关系将NULL插入为外键,doctrine-orm,Doctrine Orm,我在ZendFramework 1.11.2中使用条令2作为ORM,建立了一种双向的“一人一人”关系 注意:条令没有创建数据库表。数据库是MySQL 出于某种原因,当我将一个新的链接实体持久化并刷新到链接表(见下文)时,外键字段(container_id)被设置为NULL。但是,如果从“manytone(targetEntity=“Shepherd\Navigation\Domain\Container\Model”,inversedBy=“links”)”行中删除“@”符号,则外键字段将正确填

我在ZendFramework 1.11.2中使用条令2作为ORM,建立了一种双向的“一人一人”关系

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

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

由于删除“@”符号时实体已正确添加到数据库中,因此OneToMany关系的某些地方出现了问题

例如,如果我有一个名为$link的链接模型(请参阅下面的伪代码)

…当新链接模型被持久化并且实体管理器被刷新时,链接(shepherd_navigation_link)表中新插入行的容器id(外键)值为空

    $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)
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_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 **/
}
/**
 * @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 **/
}
注意:受保护的属性$cid映射到上面的容器id列

容器表架构:

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)
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_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 **/
}
/**
 * @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 **/
}
我错过了什么?我做错了什么?

我(通过阅读文档)解决了问题。事实证明,确实存在一些问题

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

// Inside the Link Entity class...

public function setContainer($container)
{
    $this->container = $container;
}
问题2=>我没有设置容器值。在错误中,我认为原则2在内部执行了此操作,但我发现容器变量需要在刷新之前设置

我愚蠢的疏忽

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

// $em is the Entity Manager
$em->persist($link);
$em->flush();
问题3=>需要在刷新之前保留容器($container),或者需要更改容器实体上的@OneToMany定义。我选择更新容器实体定义。查看此处()以了解更多信息

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

在做了这些更改并删除了link entity类中的@OneToOne节点关系(原来我不需要它)之后,一切都很好。我希望这对某人有所帮助。

我不认为您需要在关联映射中找到目标实体的完整路径。在某些情况下,您拥有整个路径,而在其他情况下,您只有实体名称。我不确定这是否是造成问题的原因,但你可以看看。谢谢杰里米,你是对的。我在命名模型时前后不一致。但问题依然存在。