Ho una relazione OneToMany bidirezionale usando Doctrine 2 come un ORM all'interno di ZendFramework 1.11.2.doctrine2 OneToMany relationship inserisce NULL come chiave esterna
Nota: Doctrine non ha creato le tabelle del database. Il database è MySQL.
Per qualche motivo, quando continuo a svuotare una nuova entità di collegamento nella tabella di collegamento (vedi sotto) il campo chiave esterna (id_contenitore) viene impostato su NULL. Tuttavia, se il simbolo '@' viene rimosso dalla stringa 'ManyToOne (targetEntity = "Shepherd \ Navigation \ Domain \ Container \ Model", inversedBy = "links")', il campo chiave esterna viene popolato correttamente.
Poiché l'entità viene aggiunta correttamente al database quando viene rimosso il simbolo '@', c'è qualcosa di sbagliato nella relazione OneToMany da qualche parte.
Per esempio, se ho un modello di collegamento chiamato $ link (vedi pseudo-codice qui sotto) ...
$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
}
... quando il nuovo modello di collegamento viene mantenuto e il gestore entità è arrossato, il valore container_id (chiave esterna) dalla riga appena inserita nella tabella link (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
Lo schema della tabella link:
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 entità modello:
/**
* @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 **/
}
Nota: le mappe protette CID proprietà $ alla colonna container_id sopra.
Lo schema della tabella contenitore:
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
Il modello di entità contenitore:
/**
* @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 **/
}
Che cosa mi manca? Che cosa sto facendo di sbagliato?
Non credo che tu abbia bisogno dell'intero percorso verso le tue entità bersaglio nelle tue mappature di associazione. Sembra che su alcuni tu abbia l'intero percorso e sugli altri hai solo il nome dell'entità. Non sono sicuro se questo è ciò che sta causando il problema, ma è qualcosa che potresti guardare. –
Grazie Jeremy, hai ragione. Sono incoerente nel nominare i modelli. Ma il problema persiste ancora. – user175590