php - symfony entity form type with related data -
i have created form in symfony2 form type , used entity type user choose his/her address this:
$builder->add('sladdress', 'entity', array( 'class' => 'myclass\userbundle\entity\useraddresses', 'property' => 'address', 'label' => 'label.your_addresses_list', 'translation_domain' => 'labels', 'mapped' => false ));
i have useraddresses entity has manytoone relation users entity , saves user's addresses. problem remains should sladdress type loads addresses that's owned user?(by default sladdress form field loads of addresses exist in entity) fast way?
i found solution , should use 'query_builder' argument of symfony form types this:
$builder->add('sladdress', 'entity', array('class' => 'myclass\userbundle\entity\useraddresses', 'property' => 'address', 'label' => 'label.your_addresses_list', 'translation_domain' => 'labels', 'mapped' => false, 'query_builder' => function (entityrepository $er) { return $er->createquerybuilder('u') ->where('u.user = ?1') ->setparameter(1, $this->context->gettoken()->getuser()); },));
but should note can't use $this->context in symfony formtypes should create private variable named $context in formtype :
private $context;
and create constructor method in formtype gives security context controller called $form->createfrombuilder() on form type:
public function __construct($context) { $this->context = $context; }
and when call formtype in controller passing securitycontext parameter it, this:
$form = $this->createform(new selectaddresstype($this->get('security.context')), null, array( 'action' => $this->generateurl('frontend_order_checkout', array('vendor_code' => $vendor_id)), ));
Comments
Post a Comment