php - Set PK from ManyToMany annotations in Doctrine2 entity -
php - Set PK from ManyToMany annotations in Doctrine2 entity -
i've manytomany relationship between pais
, fabricantedistribuidor
tables defined follow:
pais.php
class pais { // column definitions /** * @orm\manytomany(targetentity="appbundle\entity\fabricantedistribuidor", inversedby="paises", cascade={"persist"}) * @orm\jointable(name="negocio.fabricante_distribuidor_pais", schema="negocio", * joincolumns={@orm\joincolumn(name="fabricante_distribuidor_id", referencedcolumnname="id")}, * inversejoincolumns={@orm\joincolumn(name="pais_id", referencedcolumnname="id")} * ) */ protected $fabricantedistribuidor; /** * add together fabricantedistribuidor * * @param appbundle\entity\fabricantedistribuidor $fabricantedistribuidor */ public function addfabricantedistribuidor(\appbundle\entity\fabricantedistribuidor $fabricantedistribuidor) { $this->fabricantedistribuidor[] = $fabricantedistribuidor; } /** * fabricantedistribuidor * * @return doctrine\common\collections\collection */ public function getfabricantedistribuidor() { homecoming $this->fabricantedistribuidor; } }
fabricantedistribuidor.php
class fabricantedistribuidor { // column definitions /** * @orm\manytomany(targetentity="appbundle\entity\pais", mappedby="fabricantedistribuidor", cascade={"persist"}) */ protected $paises; public function __construct() { $this->paises = new arraycollection(); } /** * set paises * * @param appbundle\entity\pais $pais * @return fabricantedistribuidor */ public function addpaises(\appbundle\entity\pais $pais) { $this->paises[] = $pais; homecoming $this; } /** * paises * * @return doctrine\common\collections\collection */ public function getpaises() { homecoming $this->paises; } }
that generate table fabricante_distribuidor_pais
on schema negocio
fabricante_distribuidor_id
, pais_id
fk pointing pk on related tables, that's fine.
regarding scenario:
1- it's possible define fabricante_distribuidor_id
, pais_id
pk on fabricante_distribuidor_pais
table? mean adding annotation or need create external entity , set them @orm\id
on column definition?
2- addxxx
, getxxx
methods right in entities? right mean: should add together 1 or many paises
(from pais entity) fabricantedistribuidor
, don't care inverse relation meaning not add together fabricantedistribuidor
pais
, them right or need alter something?
1- if 1 id primary key doesn't relation becomes many one/ 1 many ? 1to1 if both pk 2- if don't care inverse going add together getters , setters in 1 entity yes. can still alter biredictionnal later attribute "mappedby"
check if entity exists :
you can in controller :
for illustration in pays
$data = $em->getrepository('acmebundle:pais')->findonebyfabricantedistribuidor($id); if($data) { // entity allready persisted } else { // no, can persist entity }
php symfony2 doctrine2 doctrine
Comments
Post a Comment