在Sonata Admin查询

时间:2013-11-22 17:59:01

标签: php symfony sonata-admin symfony-sonata

我正在使用Sonata创建一个后台,在configureFormFields,我想做一个查询来返回一些值。查询做得很好,并在使用var_dump时返回值,但在表单中我总是得到“类不存在”。你能救我吗?

以下是代码:

protected function configureFormFields(FormMapper $formMapper)
{
      /* @var $queryBuilder \Doctrine\ORM\QueryBuilder */
    $queryBuilder = $this->getModelManager()
            ->getEntityManager('EBCoreKernelBundle:Campaign\Campaign')
            ->createQueryBuilder();

    $queryBuilder->select('cmp.id, cmp.name')
                 ->from('EBCoreKernelBundle:Campaign\Campaign', 'cmp');

          /* @var $templateList Template[] */
    $templateList = $queryBuilder->getQuery()->execute();

         var_dump($templateList);

    $formMapper
        ->add('name','sonata_type_model',  array('required' => true, 'query' => $queryBuilder));


}

2 个答案:

答案 0 :(得分:0)

$ entity = new \ Nnx \ AbpBundle \ Entity \ Truc();

$ query = $ this-> modelManager-> getEntityManager($ entity) - > createQuery('SELECT t FROM Nnx \ AbpBundle \ Entity \ Truc t ORDER BY t.lib ASC') - > execute( );

答案 1 :(得分:0)

作为文件:

https://sonata-project.org/bundles/admin/master/doc/reference/form_types.html

  

查询默认为null。您可以将其设置为QueryBuilder实例   为了定义自定义查询以检索可用选项。

所以,让我们给它一个查询构建器:

        $queryBuilder = $this->getModelManager()
           ->getEntityManager(Category::class)
           ->createQueryBuilder('c')
           ->select('c')
           ->from('AppBundle:Category', 'c')
           ->orderBy('c.title', 'ASC')
        ;

        $formMapper->add('toto', ModelType::class, array(
           'query' => $queryBuilder
        ))