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.