Symfony creating choice from entity in form type

Tigran

I have a lot of Categories in database.

Here is Category Entity

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="categories")
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Category")
     */
    protected $rootCategory;

    /**
     * @ORM\Column(type="text")
     */
    protected $name;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Category
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set rootCategory
     *
     * @param \AppBundle\Entity\Category $rootCategory
     *
     * @return Category
     */
    public function setRootCategory(\AppBundle\Entity\Category $rootCategory = null)
    {
        $this->rootCategory = $rootCategory;

        return $this;
    }

    /**
     * Get rootCategory
     *
     * @return \AppBundle\Entity\Category
     */
    public function getRootCategory()
    {
        return $this->rootCategory;
    }
}

I want to get all categories in my edit form

EditFormType:

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use AppBundle\Controller\CategoryController;

class EditPhotoFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $categoryController = new CategoryController();

        $builder->add('title', 'text');
    $builder->add('description', 'textarea');
        $builder->add('category', EntityType::class, array(
            'class' => 'AppBundle:Category',
            'choices' => $categoryController->getCategories(),
        ));
    }

    public function getName()
    {
        return 'app_photo_edit';
    }
}

getCategories() 

public function getCategories() {
        $em = $this->getDoctrine()->getManager();

    return $em->getRepository('AppBundle:Category')->findAll();
    }

I am getting next error:

Error: Call to a member function has() on null

Thats because there is not Doctrine in controller object. Where should i get Doctrine and Repository in this case?
How should i do it correct way?

Terenoth

First, you should NEVER instantiate any Controller class yourself. Controller classes are used by Symfony's Kernel to handle a request, and they are loaded automatically with dependencies to do so.

Right here, you don't even need to require the EntityManager in your FormType, because EntityType has a built-in option query_builder to do what you need:

$builder->add('category', EntityType::class, array(
    'class' => 'AppBundle:Category',
    'query_builder' => function (EntityRepository $er) {
         return $er->createQueryBuilder('c');
    },
);

This should do the trick. (check here for more details)

However, if one day you really need to import a dependancy inside your Form (whether it is EntityManager or another service), here's how you should do:

A. import the given dependency in your constructor:

private $dependency;

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

B. Declare your Form as a Service, with your dependency's id as argument:

<service id="app.form.type.edit_photo"
         class="AppBundle\Form\Type\EditPhotoFormType">
    <tag name="form.type" />
    <argument type="service" id="app.dependencies.your_dependency" />
</service>

Then use $this->dependency in your Form wherever you need.

Hope this helps! :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

field array type in entity for form choice type field symfony

From Dev

symfony form choice with entity get result

From Dev

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

From Dev

Symfony set value checked on a form type choice

From Dev

Symfony2 choice constraint/validation on entity field type

From Dev

Manipulate the list of countries provided by Symfony Intl and the form choice type

From Dev

Pass custom value to each input in Symfony form (type is choice)

From Dev

Manipulate the list of countries provided by Symfony Intl and the form choice type

From Dev

Symfony 2 Transformer on entity form type

From Dev

symfony entity form type with related data

From Dev

Symfony 2.8 form entity type custom property

From Dev

Symfony form type entity data transformerm

From Dev

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

From Dev

symfony setRole from choice in Form end up in an error

From Dev

Symfony form from child class does not load choice options

From Dev

Symfony put entity from session to form

From Dev

Symfony 2 form property selection from entity

From Dev

Symfony put entity from session to form

From Dev

Symfony 2 form property selection from entity

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

Symfony form choice disable translator

From Dev

Symfony2: How to filter the options of an entity-choice form field by a certain attribute?

From Dev

Boolean values and choice symfony type

From Dev

Symfony Choice type with disabled options

From Dev

Symfony Choice type with disabled options

From Dev

How to render a multiple choice dropdown with option groups using "sonata_type_model" or symfony "entity"?

From Dev

Symfony2 - Creating a template for a custom form type

From Dev

symfony2 form create new type combining collection and entity

Related Related

  1. 1

    field array type in entity for form choice type field symfony

  2. 2

    symfony form choice with entity get result

  3. 3

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

  4. 4

    Symfony set value checked on a form type choice

  5. 5

    Symfony2 choice constraint/validation on entity field type

  6. 6

    Manipulate the list of countries provided by Symfony Intl and the form choice type

  7. 7

    Pass custom value to each input in Symfony form (type is choice)

  8. 8

    Manipulate the list of countries provided by Symfony Intl and the form choice type

  9. 9

    Symfony 2 Transformer on entity form type

  10. 10

    symfony entity form type with related data

  11. 11

    Symfony 2.8 form entity type custom property

  12. 12

    Symfony form type entity data transformerm

  13. 13

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

  14. 14

    symfony setRole from choice in Form end up in an error

  15. 15

    Symfony form from child class does not load choice options

  16. 16

    Symfony put entity from session to form

  17. 17

    Symfony 2 form property selection from entity

  18. 18

    Symfony put entity from session to form

  19. 19

    Symfony 2 form property selection from entity

  20. 20

    Symfony 2.7 Form Entity type render multiple properties in form

  21. 21

    Symfony 2.7 Form Entity type render multiple properties in form

  22. 22

    Symfony form choice disable translator

  23. 23

    Symfony2: How to filter the options of an entity-choice form field by a certain attribute?

  24. 24

    Boolean values and choice symfony type

  25. 25

    Symfony Choice type with disabled options

  26. 26

    Symfony Choice type with disabled options

  27. 27

    How to render a multiple choice dropdown with option groups using "sonata_type_model" or symfony "entity"?

  28. 28

    Symfony2 - Creating a template for a custom form type

  29. 29

    symfony2 form create new type combining collection and entity

HotTag

Archive