php - Doctrine2 one-to-one correctly done -
i have entity, a. , 1 b.
a={id,name} b={id,details}
i want one-to-one relation. each x there must 1 y b , reverse. thought solutions , came following.
1. id shared.
but like. a={same_id,name} b={same_id,details} same_id unique
- b have foreign key same_id a.same_id.
- or have foreign key same_id b.same_id
but want able details a. doing $a->getab()->getdetails(); means need implement 2).
but sounds counterintuitive since mean, 1 must first create new b entity , create entity , point b entity.
which looks b owns - , not want.
and in reverse - if b has foreign key a, can still tell b's existence, can $a->getab()->getdetails(); ?
2. b has id, , has foreign key it:
so like: a={id,name,bid} b={bid,details} bid unique, id unique
but in way - seems wasting column a. b won't know it's attached perhaps bad practice.
edit: doesn't solve problem, noticed must again first create b, since x needs point valid y b.
3. problem automatic generation of mappings?
currently generating xml mappings , entity classes mysql database.
could generation won't automatically tell if b has foreign key it?
one-to-one, bidirectional association you're looking (something example in point 2).
, yes - owning important. please read concepts of owning , inverse sides of associations.
i'd in example a
owning side - has name. name more significant details.
there's no problem create both entities @ same time;
$a = new a(); $a->setname("alpha"); $b = new b(); $b->setdetails("alpha goes first"); $a->setb($b); $em->persist($a)->flush();
and both entities aware of counterpart:
$a->getb(); $b->geta();
again - there must owning side , must maintain logic.
Comments
Post a Comment