doctrine2 Отношение OneToMany вставляет NULL в качестве внешнего ключа

У меня двусторонние отношения OneToMany, использующие Doctrine 2 как ORM в ZendFramework 1.11.2.

Примечание: Doctrine не создавала таблицы базы данных. База данных - это MySQL.

По какой-то причине, когда я сохраняю и сбрасываю новый объект ссылки в таблицу ссылок (см. Ниже), поле внешнего ключа (container_id) становится равным NULL. Однако, если удалить символ '@' из строки 'ManyToOne (targetEntity = "Shepherd \ Navigation \ Domain \ Container \ Model", reverseBy = "links")', поле внешнего ключа заполнено правильно.

Поскольку объект правильно добавляется в базу данных при удалении символа «@», что-то не так с отношением OneToMany.

Например, если у меня есть модель ссылок с именем $ link (см. Псевдо- код ниже) ...

 $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
    }

... когда новая модель ссылок сохраняется и диспетчер сущностей сбрасывается, значение container_id (внешний ключ) из вновь вставленной строки в таблице ссылок (shepherd_navigation_link) равно 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 **/
}

Что я скучаю? Что я делаю не так?

5
задан Daniel LeCheminant 16 March 2011 в 22:03
поделиться