1

I read the others questions on this topic, but my app still doesn't work :(

Here is my code:

class ChooseCatType extends AbstractType
{
    protected $user;

    function __construct(\EM\MyFriendsBundle\Entity\User  $user) 
    {
        $this->user = $user;
    }


     public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('name', 'entity', array(
            'class' => 'EMMyFriendsBundle:Category',
            'property' => 'name',
            'query_builder' => function ($repository) use ($user)
                { return $repository->createQueryBuilder('cat')
                                    ->select('cat')
                                    ->where('cat.user', ':user')
                                    ->setParameter('user', $user);
                },
                        ));
    }

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

This is in my controller:

$cat = new Category();

        $dd_form = $this->createForm(new \EM\MyFriendsBundle\Entity\ChooseCatType($user), $cat);

but I get an error:

Notice: Undefined variable: user in C:\xampp\htdocs\MyFriends\src\EM\MyFriendsBundle\Entity\ChooseCatType.php line 24

which is the line with the use statement. There is a difference between my code and the answers to the other related questions that they use a concrete repository, but I think that this shouldn't be the problem. Or it is? Please help me to find the solution.

1
  • excellent question... Helped a lot. Commented Nov 12, 2012 at 11:09

1 Answer 1

2

The error is telling you all you need to know: you are using a variable which has never been defined ($user). Just add this before the $builder->add line:

$user = $this->user;
Sign up to request clarification or add additional context in comments.

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.