symfony - display the name but insert the id whit entity field type -
the form entity services
namespace main\homebundle\form; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolverinterface; class servicestype extends abstracttype { /** * @param formbuilderinterface $builder * @param array $options */ public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('techid', 'entity', array( 'class' => 'mainhomebundle:technical', 'property' => 'id', )) ->add('status', 'choice', array( 'choices' => array( 'pending' => 'pending', 'finished' => 'finished', ), 'multiple' => false), array('required'=>true)) ->add('payment', 'text', array('required'=>true)) ->add('endtime', 'datetime', array('required'=>true)) ->add('send', 'submit') ; } /** * @param optionsresolverinterface $resolver */ public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'data_class' => 'main\homebundle\entity\services' )); } /** * @return string */ public function getname() { return 'main_homebundle_services'; } }
use method display select users in update form. problem when data sent , send name , want send id while showing names in select.
not use relationships in database or entities
entity services the technical id want inserted in techid of services
class services { /** * @var integer * * @orm\column(name="id", type="integer", nullable=false) * @orm\id * @orm\generatedvalue(strategy="identity") */ private $id; /** * @var integer * * @orm\column(name="customer_id", type="integer", nullable=false) */ private $customerid; /** * @var integer * * @orm\column(name="tech_id", type="integer", nullable=true) */ private $techid;
techid linking entity
class technical { /** * @var integer * * @orm\column(name="id", type="integer", nullable=false) * @orm\id * @orm\generatedvalue(strategy="identity") */ private $id; /** * @var string * * @assert\notblank() * @assert\length( * min = 8, * max = 55, * minmessage = "{{ limit }}", * maxmessage = "{{ limit }}" * ) * * @orm\column(name="name", type="string", length=55, nullable=false) */ private $name; public function __tostring() { return $this->nombre; }
only informative way because there no relationship in database or entities
an entity field wrong thing use in case - form try map instance of technical class onto services's integer $techid field, cannot work.
you need use standard choice field , pass list of (technical entity) id choices buildform() option.
in form:
public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('techid', 'choice', array( 'choices' => $options['techchoices'], )) ... } public function configureoptions(optionsresolver $resolver) { $resolver->setdefaults(array( 'techchoices' => array(), )); }
in controller:
$repository = $this->getdoctrine()->getrepository('mainhomebundle:technical'); $technicals = $repository->findall(); $techchoices = array(); foreach($technicals $t) { $techchoices[$t->getid()] = $t->getname(); } $formoptions = array('techchoices' => $techchoices); $form = $this->createform(new servicestype(), $aservices, $formoptions);
Comments
Post a Comment