php - One-To-Many relationship deletes foreign key in Doctrine -
this driving me crazy.
a client
can have many vehicle
s.
this 1 many relationship. when trying save entities error saying foreign key null. when remove doctrine relation , store vehicle
separately working fine.
this how created relation:
class vehicle { ... /** * @orm\manytoone(targetentity="client", inversedby="vehicles") * @orm\joincolumn(name="client_id", referencedcolumnname="id") */ public $client; } class client { ... public function __construct() { parent::__construct(); $this->vehicles = new \doctrine\common\collections\arraycollection(); } /** * @orm\onetomany(targetentity="vehicle", mappedby="client", cascade={"persist"}) */ private $vehicles; }
i try save entities this:
$client = new client(); $vehicle = new vehicle(); $client->getvehicles()->add($vehicle); $em->persist($client); $em->flush();
next pdo exception saying client_id
can't null
on vehicle
table.
it seems doctrine not copying foreign key correctly.
what doing wrong?
according their docs:
it not possible use join columns pointing non-primary keys.
doctrine think these primary keys , create lazy-loading proxies data, can lead unexpected results. doctrine can performance reasons not validate correctness of settings @ runtime through validate schema command.
obviously need different approach.
a solution given in example:
create table product ( id integer, name varchar, primary key(id) ); create table product_attributes ( product_id integer, attribute_name varchar, attribute_value varchar, primary key (product_id, attribute_name) );
this schema should mapped product entity follows:
class product { private $id; private $name; private $attributes = array(); }
where
attribute_name
column contains key ,attribute_value
contains value of each array element in$attributes
.
Comments
Post a Comment