Mam dwukierunkową relację OneToMany, używając Doctrine 2 jako ORM w ZendFramework 1.11.2.doktryna2 Zależność OneToMany wstawia NULL jako klucz obcy
Uwaga: Doctrine nie utworzył tabel bazy danych. Baza danych to MySQL.
Z jakiegoś powodu, gdy utrzymuję się i przepłukuję nowy podmiot łączący do tabeli linków (patrz poniżej), pole klucza obcego (id_pojemnika) zostaje ustawione na NULL. Jeśli jednak symbol "@" zostanie usunięty z wiersza "ManyToOne (targetEntity =" Shepherd \ Navigation \ Domain \ Model ", inversedBy =" links "), pole klucza obcego zostanie poprawnie wypełnione.
Ponieważ encja jest poprawnie dodawana do bazy danych po usunięciu symbolu "@", coś jest nie tak ze związkiem OneToMany.
Na przykład, jeśli mam modelu łącza o nazwie $ Link (patrz pseudo-kod poniżej) ...
$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
}
... kiedy nowy model ogniwo jest zachowywane i kierownik jednostka jest zaczerwieniona, wartość container_id (klucz obcy) z nowo wstawionego wiersza tabeli link (shepherd_navigation_link) ma wartość 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
Link tabeli schematu:
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)
link jednostka model:
/**
* @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 **/
}
Uwaga: Właściwość chroniona $ mapy CID do kolumny container_id powyżej.
Pojemnik tabeli schematu:
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
Pojemnik jednostka model:
/**
* @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 **/
}
Czego mi brakuje? Co ja robię źle?
Nie wierzę, że potrzebujesz całej ścieżki do swoich jednostek docelowych w swoich powiązaniach asocjacyjnych. Wygląda na to, że na niektórych masz całą ścieżkę, a na innych masz tylko nazwę jednostki. Nie jestem pewien, czy to właśnie jest przyczyną problemu, ale jest to coś, na co możesz spojrzeć. –
Dzięki Jeremy, masz rację. Jestem niekonsekwentny w nazywaniu modeli. Ale problem nadal istnieje. – user175590