0

I'm trying to access a list of categories I have in a database and put them into a form in Symfony2.

public function productAddAction()
    {
        $product = new Product();
        $categories = $this->getDoctrine()
            ->getRepository('AudsurShopBundle:Category')
            ->findAll();

        $form = $this->createFormBuilder($product)
            ->add('category', 'choice', array(
                'choices' => $categories, /* this is wrong */
                'multiple'  => false,
            ))
            ->add('name', 'text')
            ->add('save', 'submit', array('label' => 'Create Task'))
            ->getForm();

        return $this->render('AudsurAdminBundle:Default:new.html.twig', array(
            'form' => $form->createView(),
        ));
    }

How do I go from $categories to an object that I can put into the following part, and it complying with what the function expects?

->add('category', 'choice', array(
                    'choices' => $categories, /* this is wrong */
                    'multiple'  => false,
                ))

I know this is basic, but I can't seem to find the right keywords to find the answer (what should I have looked for?)

3 Answers 3

2

First off, "this is wrong" is not a specific error message for us to help you very much. It's like saying "my code doesn't work" and not telling us why. Moving on to your actual problem..

You're not using the right Form type to handle the entity type and display it properly. As @Talus mentioned, the Field Type you need is entity. There's a few things you're missing, such as the class parameter and the property parameter (assuming you haven't written a __toString() function in the Entity class.)

    $categories = $this->getDoctrine()
        ->getRepository('AudsurShopBundle:Category')
        ->findAll();

    $form = $this->createFormBuilder($product)
        ->add('category', 'entity', array(
            'class' => 'AudsurShopBundle:Category',
            'choices' => $categories,
            'multiple'  => false,
            'property' => 'name', // This needs to be an actual property, I just assumed name
        ))
        ->add('name', 'text')
        ->add('save', 'submit', array('label' => 'Create Task'))
        ->getForm();

Since you're using all Category entities that exist, the findAll() query is actually not necessary. You can instead go for the basic usage:

    $form = $this->createFormBuilder($product)
        ->add('category', 'entity', array(
            'class' => 'AudsurShopBundle:Category',
            'multiple'  => false,
            'property' => 'name', // This needs to be an actual property, I just assumed name
        ))
        ->add('name', 'text')
        ->add('save', 'submit', array('label' => 'Create Task'))
        ->getForm();

If you're looking for a specific subset of the Categories, you can use the choices property like before or pass a query_builder.

Sign up to request clarification or add additional context in comments.

Comments

2

IRC, I think there is a type for that : http://symfony.com/fr/doc/current/reference/forms/types/entity.html

This should help to whatever you want to do, if I understood what you meant. :)

1 Comment

Not sure if that's what I mean. I know there are form functions for lists. But once I have retrieved my Categories from the database, hwo do I change that object into something that I can put into that form function?
0

Well you can parse an Array from database:

Create a method in repository findAllArray():

public function findAllArray() {

    return $this->getEntityManager()
        ->createQuery(
            'SELECT Category '.
            'FROM AudsurShopBundle:Category AS Category'
        )
        ->getArrayResult();
}

Then call it in your controller and get all categories:

$categories = $this->getDoctrine()
    ->getRepository('AudsurShopBundle:Category')
    ->findAllArray();    

When you have got array, make it suitable for choice (create new array $choices):

$choices = array();
foreach ($categories as $category) {
    $choices[$value['id']] = $value['title'];
}

Then put this new array into form:

->add('category', 'entity', array(
        'choices' => $choices,

Hope it helped. Have a nice day.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.