I have a OneToMany bidirectional relationship using Doctrine 2 as an ORM in ZendFramework 1.11.2.
Note. Doctrine did not create database tables. The database is MySQL.
For some reason, when I save and delete a new link object in the link table (see below), the foreign key field (container_id) gets NULL. However, if the "@" character is removed from the line "ManyToOne (targetEntity =" Shepherd \ Navigation \ Domain \ Container \ Model ", inversedBy =" links ") ', the foreign key field is filled in properly.
Since the object is added properly to the database when the "@" character is deleted, something is wrong with the OneToMany relationship.
For example, if I have a link model named $ link (see pseudo code below) ...
$link (Shepherd\Navigation\Domain\Link\Model)
{
id: ''
cid: 23
label: test
uri: test.com
...
}
... when the new link model is saved and the object manager is cleared, the value of container_id (foreign key) from the newly inserted row in the link table (shepherd_navigation_link) is NULL.
$em
$em->persist($link);
$em->flush();
Link table layout:
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)
Communication object model:
class
{
protected $id;
protected $cid;
protected $nid;
protected $pid;
protected $label;
protected $options;
protected $events;
protected $privilege;
protected $resource;
protected $uri;
protected $visible;
private $children;
private $parent;
private $container;
private $node;
public function __construct()
{
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
}
Note. The protected $ cid property appears in the container_id column above.
Container table layout:
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
Container Object Model:
class Model
{
protected $id;
protected $name;
protected $description;
private $links;
public function __construct()
{
$this->links = new \Doctrine\Common\Collections\ArrayCollection();
}
}
What am I missing? What am I doing wrong?