1
votes

I have a problem when I have to insert Entity data in the database.

The entities are showed with the entity field of Symfony2 and I'm using the Query Builder to show only data that is "active". (see below)

Data can be added to the "select" generated by Symfony via Ajax (new "profs" that aren't active (is_active = 0)). But when I select data, added via Ajax and that's not active, I have this error: "This value is not valid." => that's because Symfony2 adds automatic validation due to de querybuilder "where" statement.

Is it possible to override the default Symfony2 behaviour, so that also not active (is_active = 0 (boolean)) -added elements via Ajax- elements can be validated (items that doesn't match the query builder "where" statement below?

$builder->add('professionA', 'genemu_jquerychosen_entity', array(
    'class' => 'SportUserBundle:Profession',
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('prof')
            ->where('prof.is_active = :active')
            ->setParameters(array('active' => '1'))
            ->orderBy('prof.name', 'ASC');
    },
));

Thanks!

1
Can you show the source code of the select after including one teacher with ajax (thanks to firebug ?)Sybio
I would pass an option to the form, to tell whether the query_builder has to select only active Profession.AdrienBrault
yes but the problem is that via Ajax I add profession that aren't active and this conflicts with the query_builder statement that should know in advance all possible choices I think?benske

1 Answers

0
votes

The easiest way is to initially register all possible elements in that field. That is, get all profession data from database and then recreate or limit the field content using ajax when the document is ready and when active state is changed.

'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('prof')->orderBy('prof.name', 'ASC');
}

Not a very fine solution I guess, but it simply works. :)