سؤال

I have a form in my symfony2 project where I am adding a new form element to the builder:

$builder->add('section', 'entity', array(
            'class' => 'SciForumVersion2Bundle:Section',
            'label' => 'Section',
            'property' => 'name',
            'query_builder' => function(EntityRepository $er) {
                return $er->createQueryBuilder('s')
                ->where('s.conference = conference')
                ->setParameter('conference', $conference);
            },
    ));

And the $conference variable comes from the controller and I am setting this variable in the FormType:

private $conference;

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

But then I am getting the error message:

ContextErrorException: Notice: Undefined variable: conference in ....

And the line number is pointing to the line:

->setParameter('conference', $conference);

When using

'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('s')
        ->where('s.conference = :conference')
        ->setParameter('conference', $this->conference);
    },

I am getting this error message:

FatalErrorException: Error: Using $this when not in object context in..

Any idea? Thank you.

هل كانت مفيدة؟

المحلول

In your FormType __construct(), you have a mistake in the class variable name to set :

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

In the query_builder parameter, use a class variable for value, and prefix parameter name with a ':' in the DQL part :

$conference = $this->conference;
$builder->add('section', 'entity', array(
        'class' => 'SciForumVersion2Bundle:Section',
        'label' => 'Section',
        'property' => 'name',
        'query_builder' => function(EntityRepository $er) use ($conference) {
            return $er->createQueryBuilder('s')
            ->where('s.conference = :conference')
            ->setParameter('conference', $conference);
        },
));

نصائح أخرى

I thin this line is producing error

->where('s.conference = $conference')

i would be like that

->where('s.conference = conference')
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top