symfony - Symfony2 Doctrine2 trouble with optional one to one relation -


i have problem doctrine2 in symfony2 , 2 relationed entities.

there user-entity can (not must) have usermeta-entity referenced contains information biography etc.

the usermeta optional because user imported system, while usermeta managed in application.

of course want save both together, saving user must create or update usermeta-entity.

both joined column named aduserid (same name in both tables).

i've recognized if usermeta optional reference owning-side in case should usermeta, otherwise doctrine loads user , needs usermeta entity - it's not there.

please note comments in user->setmeta..

/**  * user  *  * @orm\table(name="user")  * @orm\entity  */ class user { /**  * @var usermeta  * @orm\onetoone(targetentity="usermeta", mappedby="user", cascade={"persist"})  */ protected $meta;  public function getmeta() {     return $this->meta; }  /**  *   * @param usermeta $metavalue   */ public function setmeta($metavalue) {         // i've tried setting join-column-value here  //  - it's not getting persisted // $metavalue->setaduserid($this->getaduserid());  // i've tried set user-object in usermeta -  //  seems doctrine wants update usermeta , searches //  valid names aduserid (in basicentitypersister->_prepareupdatedata)  //  id given -  not luck here // $metavalue->setuser($this);                 $this->meta = $metavalue; }  /**  * @var integer  *  * @orm\column(name="rowid", type="integer", nullable=false)  * @orm\id  * @orm\generatedvalue(strategy="identity")  */ private $id;   /**  * rowid  *  * @return integer   */ public function getid() {     return $this->id; }  /**  * @var integer  *  * @orm\column(name="aduserid", type="integer", nullable=false)  */ private $aduserid;  /**  * set aduserid  *  * @param integer $aduserid  * @return user  */ public function setaduserid($aduserid) {     $this->aduserid = $aduserid;      return $this; }  /**  * aduserid  *  * @return integer   */ public function getaduserid() {     return $this->aduserid; }  // mor fields....  } 

and usermeta class:

/**  * usermeta  *  * @orm\table(name="usermeta")  * @orm\entity  */ class usermeta { /**  * @orm\onetoone(targetentity="user", inversedby="meta")  * @orm\joincolumn(name="aduserid", referencedcolumnname="aduserid")  */ protected $user;  public function getuser() {     return $this->$user; }      public function setuser($userobj) {     $this->user = $userobj; }  /**  * @var integer  *  * @orm\column(name="id", type="integer", nullable=false)  * @orm\id  * @orm\generatedvalue(strategy="identity")  */ private $id;  /**  * @var integer  *  * @orm\column(name="aduserid", type="integer", nullable=false)  */ private $aduserid;  /**  * set aduserid  *  * @param integer $aduserid  * @return user  */ public function setaduserid($aduserid) {     $this->aduserid = $aduserid;      return $this; }  /**  * aduserid  *  * @return integer   */ public function getaduserid() {     return $this->aduserid; } } 

the controller code looks this:

...  $userform->bind($request);      if($userform->isvalid()) {         $em->persist($user);         $em->flush();     } ... 

the zdenek machek comment correct. can see doctrine2 documentation, nullable option should in join annotation (@joincolumn), not in mapping 1 (@onetoone).

@joincolumn doc:

this annotation used in context of relations in @manytoone, @onetoone fields , in context of @jointable nested inside @manytomany. annotation not required. if not specified attributes name , referencedcolumnname inferred table , primary key names.

required attributes:

name: column name holds foreign key identifier relation. in context of @jointable specifies column name in join table.

referencedcolumnname: name of primary key identifier used joining of relation.

optional attributes:

unique: determines if relation exclusive between affected entities , should enforced on database constraint level. defaults false.

nullable: determine if related entity required, or if null allowed state relation. defaults true.

ondelete: cascade action (database-level)

onupdate: cascade action (database-level)

columndefinition: ddl sql snippet starts after column name , specifies complete (non-portable!) column definition. attribute allows make use of advanced rmdbs features. using attribute on @joincolumn necessary if need different column definitions joining columns, example regarding null/not null defaults. default “columndefinition” attribute on @column sets related @joincolumn’s columndefinition. necessary make foreign keys work.

http://doctrine-orm.readthedocs.org/en/latest/reference/annotations-reference.html#annref-joincolumn

@onetoone doc:

the @onetoone annotation works @manytoone 1 additional option can specified. configuration defaults @joincolumn using target entity table , primary key column names apply here too.

required attributes:

targetentity: fqcn of referenced target entity. can unqualified class name if both classes in same namespace. important: no leading backslash!

optional attributes:

cascade: cascade option

fetch: 1 of lazy or eager

orphanremoval: boolean specifies if orphans, inverse onetoone entities not connected owning instance, should removed doctrine. defaults false.

inversedby: inversedby attribute designates field in entity inverse side of relationship.

http://doctrine-orm.readthedocs.org/en/latest/reference/annotations-reference.html#onetoone


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -