-1

I'm trying to build some kind of complex query using Doctrine Query Builder to use them in a Form Type, see below what I have done:

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('category', 'entity', array(
                'class' => 'CategoryBundle:Category',
                'property' => 'name',
                'required' => false,
                'multiple' => true,
                'expanded' => false,
                'query_builder' => function(EntityRepository $er) {
                    $qb = $er->createQueryBuilder('c')
                            ->where($qb->expr()->eq('c.parent', '?1'), $qb->expr()->isNull('c.parent'))
                            ->setParameter(1, 0);
                }
            ))
            ->add('detail_group', 'entity', array('class' => 'ProductBundle:DetailGroup', 'property' => 'name', 'required' => false, 'multiple' => true, 'expanded' => false))
            ->add('parent', 'entity', array('class' => 'ProductBundle:ProductDetail', 'property' => 'label', 'required' => false))
            ->add('label')
            ->add('field_type', 'choice', ['choices' => \ProductBundle\DBAL\Types\FieldType::getChoices()])
            ->add('values_text', 'collection', array('type' => 'text', 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false))
            ->add('description', 'text', array('required' => false))
            ->add('measure_unit', 'text', array('required' => false))
            ->add('status', 'choice', ['choices' => \ProductBundle\DBAL\Types\StatusType::getChoices()])
            ->add('to_product', 'checkbox', array('label' => 'Detalle de Stock?', 'required' => false));
}

I need to get in that query all the rows from category table with parent=NULL or parent=0 but it's not working since I get this error:

FatalErrorException: Error: Call to a member function expr() on a non-object in /var/www/html/src/ProductBundle/Form/ProductDetailType.php line 22

What I'm doing wrong?

4

1 回答 1

2

那是因为$qb不存在。

$qb = $er->createQueryBuilder('c');
$qb->where($qb->expr()->eq('c.parent', '?1'), $qb->expr()->isNull('c.parent'))
   ->setParameter(1, 0);
于 2013-09-05T15:46:54.650 回答