symfony entity form type with related data

Mohammad

i have created my form in symfony2 form type and i used an entity type for user to choose his/her address from it like 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 an UserAddresses Entity which has a ManyToOne relation to the Users Entity and saves the user's Addresses. the problem remains is that what should i do so that sladdress type loads only the Addresses that's are owned to the user?(By default the sladdress form field loads all of the addresses that exist in this entity) what is the fast way?

Mohammad

i found the solution , we should use the 'query_builder' argument of the symfony form types like 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 what should we note is that we can't use $this->context in symfony formTypes so we should create a private variable named $context in this formType like this :

private $context;

and then create a constructor method in this formType that gives the security context from the controller that called the $form->createFromBuilder() on this form type:

public function __construct($context)
{
    $this->context = $context;
}

and finally when we call the FormType in the controller with passing the SecurityContext as parameter to it, like this:

    $form = $this->createForm(new SelectAddressType($this->get('security.context')), null, array(
        'action' => $this->generateUrl('frontend_order_checkout', array('vendor_code' =>  $vendor_id)),
    ));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony form type entity data transformerm

From Dev

Symfony2 form - Remove related entity form on submit

From Dev

Symfony 2 Transformer on entity form type

From Dev

Symfony creating choice from entity in form type

From Dev

Symfony 2.8 form entity type custom property

From Dev

Symfony form for a general entity with a type and different options

From Dev

Symfony unmapped entity form field not having data

From Dev

Symfony 2.3 pass custom data to entity form, using choice or other type

From Dev

Symfony 2.7 Form Entity type render multiple properties in form

From Dev

Symfony 2.7 Form Entity type render multiple properties in form

From Dev

field array type in entity for form choice type field symfony

From Dev

symfony2 form create new type combining collection and entity

From Dev

Additional properties to entity Field Type in a form in Symfony2

From Dev

Symfony2 entity form type not saving many to many

From Dev

Symfony2 - How to validate autocomplete entity form type?

From Dev

Symfony2 form type entity add extra option

From Dev

Adding a default value along with entity in form type of Symfony2

From Dev

Symfony3 form type, reference other entity by id

From Dev

Symfony update entity with form

From Dev

Symfony2.6: Form handle a request with array data instead entity

From Dev

How to write the data of my entity in my form in Symfony 2

From Dev

Nullable custom form entity with Symfony

From Dev

Symfony Form Collection with Singular Entity

From Dev

Symfony validate entity using form

From Dev

Eager loading of related entity in Symfony 2

From Dev

Symfony 2 Doctrine persist Entity with related entities

From Dev

Entity Framework scaffold and related data

From Dev

symfony2 - How to create a form field of type "entity" without values

From Dev

Symfony3.0 When saving a form with Entity Type dropdown; SQL error cannot insert null

Related Related

  1. 1

    Symfony form type entity data transformerm

  2. 2

    Symfony2 form - Remove related entity form on submit

  3. 3

    Symfony 2 Transformer on entity form type

  4. 4

    Symfony creating choice from entity in form type

  5. 5

    Symfony 2.8 form entity type custom property

  6. 6

    Symfony form for a general entity with a type and different options

  7. 7

    Symfony unmapped entity form field not having data

  8. 8

    Symfony 2.3 pass custom data to entity form, using choice or other type

  9. 9

    Symfony 2.7 Form Entity type render multiple properties in form

  10. 10

    Symfony 2.7 Form Entity type render multiple properties in form

  11. 11

    field array type in entity for form choice type field symfony

  12. 12

    symfony2 form create new type combining collection and entity

  13. 13

    Additional properties to entity Field Type in a form in Symfony2

  14. 14

    Symfony2 entity form type not saving many to many

  15. 15

    Symfony2 - How to validate autocomplete entity form type?

  16. 16

    Symfony2 form type entity add extra option

  17. 17

    Adding a default value along with entity in form type of Symfony2

  18. 18

    Symfony3 form type, reference other entity by id

  19. 19

    Symfony update entity with form

  20. 20

    Symfony2.6: Form handle a request with array data instead entity

  21. 21

    How to write the data of my entity in my form in Symfony 2

  22. 22

    Nullable custom form entity with Symfony

  23. 23

    Symfony Form Collection with Singular Entity

  24. 24

    Symfony validate entity using form

  25. 25

    Eager loading of related entity in Symfony 2

  26. 26

    Symfony 2 Doctrine persist Entity with related entities

  27. 27

    Entity Framework scaffold and related data

  28. 28

    symfony2 - How to create a form field of type "entity" without values

  29. 29

    Symfony3.0 When saving a form with Entity Type dropdown; SQL error cannot insert null

HotTag

Archive