php - Symfony [2.8.9] EntityType within embedded form not selecting value -
i have organisation entity referencing embedded form containing country entity. form saves without issue, country entitytype field not correctly pick value upon editing, , such first option in dropdown selected rather correct option.
the organisationtype form:
<?php namespace appbundle\form\type\meta; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\form\formevent; use symfony\component\form\formevents; use symfony\component\optionsresolver\optionsresolver; class organisationtype extends abstracttype { public function __construct() { } public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('title', new titletype(), array('required' => true, 'label' => false)) ->add('country', new countrytype(), array('label' => false)) ->add('location', 'text', array('required' => false, 'label' => 'city')); } public function getname() { return 'organisation'; } public function configureoptions(optionsresolver $resolver) { $resolver->setdefaults(array( 'data_class' => 'appbundle\entity\organisation', 'csrf_protection' => true, 'csrf_field_name' => '_token', // unique key generate secret token 'intention' => 'organisation', 'cascade_validation' => true )); } }
the countrytype form:
<?php namespace appbundle\form\type\meta; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolver; use symfony\bridge\doctrine\form\type\entitytype; use doctrine\orm\entityrepository; class countrytype extends abstracttype { public function __construct() { } public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('id', entitytype::class, array( 'label' => 'country', 'class' => 'appbundle:country', 'choice_label' => 'name', 'placeholder' => 'choose option...', 'query_builder' => function (entityrepository $repository) { return $repository->createquerybuilder('c')->orderby('c.name', 'asc'); } )); } public function getname() { return 'country'; } public function configureoptions(optionsresolver $resolver) { $resolver->setdefaults(array( 'data_class' => 'appbundle\entity\country' )); } }
organisation entity:
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert; use symfony\bridge\doctrine\validator\constraints\uniqueentity; use gedmo\mapping\annotation gedmo; use appbundle\model\organisationmodel; /** * organisation * * @orm\table(name="organisation", indexes={@orm\index(name="fk_meta_org_country_idx", columns={"country_id"}), @orm\index(name="fk_meta_org_subdivision_idx", columns={"country_subdivision_id"}), @orm\index(name="fk_meta_org_user_entered_idx", columns={"entered_by_user_id"}), @orm\index(name="fk_meta_org_user_updated_idx", columns={"updated_by_user_id"}), @orm\index(name="fk_meta_org_title_idx", columns={"title_id"})}) * @orm\entity(repositoryclass="appbundle\entity\organisationrepository") */ class organisation extends organisationmodel { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="identity") */ private $id; /** * @var string * * @orm\column(name="location", type="string", length=45, nullable=false) */ private $location; /** * @var boolean * * @orm\column(name="hidden", type="boolean", nullable=false) */ private $hidden; /** * @var \datetime * * @orm\column(name="date_entered", type="datetime", nullable=false) * @gedmo\timestampable(on="create") */ private $dateentered; /** * @var \datetime * * @orm\column(name="date_updated", type="datetime", nullable=false) * @gedmo\timestampable(on="update") */ private $dateupdated; /** * @var \appbundle\entity\country * * @orm\manytoone(targetentity="appbundle\entity\country") * @orm\joincolumns({ * @orm\joincolumn(name="country_id", referencedcolumnname="id") * }) */ private $country; /** * @var \appbundle\entity\countrysubdivision * * @orm\manytoone(targetentity="appbundle\entity\countrysubdivision") * @orm\joincolumns({ * @orm\joincolumn(name="country_subdivision_id", referencedcolumnname="id") * }) */ private $countrysubdivision; /** * @var \appbundle\entity\title * * @orm\manytoone(targetentity="appbundle\entity\title", cascade="persist") * @orm\joincolumns({ * @orm\joincolumn(name="title_id", referencedcolumnname="id") * }) * @assert\type(type="appbundle\entity\title") * @assert\valid() */ private $title; /** * @var \appbundle\entity\user * * @orm\manytoone(targetentity="appbundle\entity\user") * @orm\joincolumns({ * @orm\joincolumn(name="entered_by_user_id", referencedcolumnname="id") * }) */ private $enteredbyuser; /** * @var \appbundle\entity\user * * @orm\manytoone(targetentity="appbundle\entity\user") * @orm\joincolumns({ * @orm\joincolumn(name="updated_by_user_id", referencedcolumnname="id") * }) */ private $updatedbyuser; /** * id * * @return integer */ public function getid() { return $this->id; } /** * set location * * @param string $location * @return organisation */ public function setlocation($location) { $this->location = $location; return $this; } /** * location * * @return string */ public function getlocation() { return $this->location; } /** * set hidden * * @param boolean $hidden * @return organisation */ public function sethidden($hidden) { $this->hidden = $hidden; return $this; } /** * hidden * * @return boolean */ public function gethidden() { return $this->hidden; } /** * set dateentered * * @param \datetime $dateentered * @return organisation */ public function setdateentered($dateentered) { $this->dateentered = $dateentered; return $this; } /** * dateentered * * @return \datetime */ public function getdateentered() { return $this->dateentered; } /** * set dateupdated * * @param \datetime $dateupdated * @return organisation */ public function setdateupdated($dateupdated) { $this->dateupdated = $dateupdated; return $this; } /** * dateupdated * * @return \datetime */ public function getdateupdated() { return $this->dateupdated; } /** * set country * * @param \appbundle\entity\country $country * @return organisation */ public function setcountry(\appbundle\entity\country $country = null) { $this->country = $country; return $this; } /** * country * * @return \appbundle\entity\country */ public function getcountry() { return $this->country; } /** * set countrysubdivision * * @param \appbundle\entity\countrysubdivision $countrysubdivision * @return organisation */ public function setcountrysubdivision(\appbundle\entity\countrysubdivision $countrysubdivision = null) { $this->countrysubdivision = $countrysubdivision; return $this; } /** * countrysubdivision * * @return \appbundle\entity\countrysubdivision */ public function getcountrysubdivision() { return $this->countrysubdivision; } /** * set title * * @param \appbundle\entity\title $title * @return organisation */ public function settitle(\appbundle\entity\title $title = null) { $this->title = $title; return $this; } /** * title * * @return \appbundle\entity\title */ public function gettitle() { return $this->title; } /** * set enteredbyuser * * @param \appbundle\entity\user $enteredbyuser * @return organisation */ public function setenteredbyuser(\appbundle\entity\user $enteredbyuser = null) { $this->enteredbyuser = $enteredbyuser; return $this; } /** * enteredbyuser * * @return \appbundle\entity\user */ public function getenteredbyuser() { return $this->enteredbyuser; } /** * set updatedbyuser * * @param \appbundle\entity\user $updatedbyuser * @return organisation */ public function setupdatedbyuser(\appbundle\entity\user $updatedbyuser = null) { $this->updatedbyuser = $updatedbyuser; return $this; } /** * updatedbyuser * * @return \appbundle\entity\user */ public function getupdatedbyuser() { return $this->updatedbyuser; } }
country entity:
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use doctrine\common\collections\arraycollection; use doctrine\common\collections\criteria; /** * country * * @orm\table(name="country", uniqueconstraints={@orm\uniqueconstraint(name="unique", columns={"code"})}) * @orm\entity(repositoryclass="appbundle\entity\countryrepository") */ class country { /** * @var string * * @orm\column(name="code", type="string", length=2, nullable=false) */ private $code; /** * @var string * * @orm\column(name="code_iso_3", type="string", length=3, nullable=true) */ private $codeiso3; /** * @var string * * @orm\column(name="code_iso_numeric", type="string", length=4, nullable=true) */ private $codeisonumeric; /** * @var string * * @orm\column(name="name", type="string", length=45, nullable=false) */ private $name; /** * @var string * * @orm\column(name="capital", type="string", length=30, nullable=true) */ private $capital; /** * @var string * * @orm\column(name="continent_name", type="string", length=15, nullable=true) */ private $continentname; /** * @var string * * @orm\column(name="continent_code", type="string", length=2, nullable=true) */ private $continentcode; /** * @var boolean * * @orm\column(name="hidden", type="boolean", nullable=false) */ private $hidden; /** * @var \datetime * * @orm\column(name="date_entered", type="datetime", nullable=false) */ private $dateentered; /** * @var \datetime * * @orm\column(name="date_updated", type="datetime", nullable=false) */ private $dateupdated; /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="identity") */ private $id; /** * @var doctrine\common\collections\arraycollection * * @orm\onetomany(targetentity="countrysubdivision", mappedby="country") */ private $subdivisions; /* * @var appbundle\entity\countrysubdivision * stored purely purposes of form capture */ private $subdivision; /** * @var doctrine\common\collections\arraycollection * * @orm\onetomany(targetentity="countrysubdivisiontype", mappedby="country") */ private $subdivisiontypes; public function __construct() { $this->subdivisions = new arraycollection(); $this->subdivisiontypes = new arraycollection(); } public function getsubdivisions() { return $this->subdivisions->toarray(); } public function getsubdivisionsofparentid($parentid) { $criteria = criteria::create() ->where(criteria::expr()->eq("parentid", $parentid)) ->orderby(array("name" => criteria::asc)); return $this->subdivisions->matching($criteria)->toarray(); } public function setsubdivisions($subdivisions) { } public function getsubdivision() { return $this->subdivision; } public function setsubdivision($subdivision) { $this->subdivision = $subdivision; } public function setid($id) { $this->id = $id; } public function getsubdivisiontypeofparentid($parentid) { $criteria = criteria::create() ->where(criteria::expr()->eq("subdivisionid", $parentid)) ->orderby(array("name" => criteria::asc)); return $this->subdivisiontypes->matching($criteria)[0]; } /** * set code * * @param string $code * @return country */ public function setcode($code) { $this->code = $code; return $this; } /** * code * * @return string */ public function getcode() { return $this->code; } /** * set codeiso3 * * @param string $codeiso3 * @return country */ public function setcodeiso3($codeiso3) { $this->codeiso3 = $codeiso3; return $this; } /** * codeiso3 * * @return string */ public function getcodeiso3() { return $this->codeiso3; } /** * set codeisonumeric * * @param string $codeisonumeric * @return country */ public function setcodeisonumeric($codeisonumeric) { $this->codeisonumeric = $codeisonumeric; return $this; } /** * codeisonumeric * * @return string */ public function getcodeisonumeric() { return $this->codeisonumeric; } /** * set name * * @param string $name * @return country */ public function setname($name) { $this->name = $name; return $this; } /** * name * * @return string */ public function getname() { return $this->name; } /** * set capital * * @param string $capital * @return country */ public function setcapital($capital) { $this->capital = $capital; return $this; } /** * capital * * @return string */ public function getcapital() { return $this->capital; } /** * set continentname * * @param string $continentname * @return country */ public function setcontinentname($continentname) { $this->continentname = $continentname; return $this; } /** * continentname * * @return string */ public function getcontinentname() { return $this->continentname; } /** * set continentcode * * @param string $continentcode * @return country */ public function setcontinentcode($continentcode) { $this->continentcode = $continentcode; return $this; } /** * continentcode * * @return string */ public function getcontinentcode() { return $this->continentcode; } /** * set hidden * * @param boolean $hidden * @return country */ public function sethidden($hidden) { $this->hidden = $hidden; return $this; } /** * hidden * * @return boolean */ public function gethidden() { return $this->hidden; } /** * set dateentered * * @param \datetime $dateentered * @return country */ public function setdateentered($dateentered) { $this->dateentered = $dateentered; return $this; } /** * dateentered * * @return \datetime */ public function getdateentered() { return $this->dateentered; } /** * set dateupdated * * @param \datetime $dateupdated * @return country */ public function setdateupdated($dateupdated) { $this->dateupdated = $dateupdated; return $this; } /** * dateupdated * * @return \datetime */ public function getdateupdated() { return $this->dateupdated; } /** * id * * @return integer */ public function getid() { return $this->id; } }
if replace, in countrytype form...
->add('id', entitytype::class, ...
with...
->add('name')
...then see country name displayed without issue. going wrong entitytype?
i think you're not using custom formtype correctly. you're not supposed add builder in custom formtype. try :
<?php namespace appbundle\form\type\meta; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\form\formevent; use symfony\component\form\formevents; use symfony\component\optionsresolver\optionsresolver; class organisationtype extends abstracttype { public function __construct() { } public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('title', new titletype(), array('required' => true, 'label' => false)) ->add('country', new countrytype(), array( 'label' => 'country', 'choice_label' => 'name', 'placeholder' => 'choose option...', 'query_builder' => function (entityrepository $repository) { return $repository->createquerybuilder('c')->orderby('c.name', 'asc'); } )) ->add('location', 'text', array('required' => false, 'label' => 'city')); } public function getname() { return 'organisation'; } public function configureoptions(optionsresolver $resolver) { $resolver->setdefaults(array( 'data_class' => 'appbundle\entity\organisation', 'csrf_protection' => true, 'csrf_field_name' => '_token', // unique key generate secret token 'intention' => 'organisation', 'cascade_validation' => true )); } }
with
<?php namespace appbundle\form\type\meta; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolver; use symfony\bridge\doctrine\form\type\entitytype; use doctrine\orm\entityrepository; class countrytype extends abstracttype { public function getname() { return 'country'; } public function getparent() { return entitytype::class; } public function configureoptions(optionsresolver $resolver) { $resolver->setdefaults(array( 'data_class' => 'appbundle\entity\country' )); } }
Comments
Post a Comment