在SonataAdmin类(Symfony4)中添加查询

时间:2018-11-18 11:59:44

标签: symfony4 sonata

我无法在$ formMapper变量的ModelType :: class的查询选项中输入没有错误的查询。

我在src / Entity / Tareaspendientes.php中有

....
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Prioridad", inversedBy="tareas")
     */
    private $prioridad;

在src / Entity / Prioridad.php中:

....
    /**
     * @var string
     *
     * @ORM\Column(name="prioridad", type="string", length=80)
     */
    private $prioridad;

以及src / Admin / TareaspendientesAdmin.php中的

....
use App\Entity\Tareaspendientes;
use App\Entity\Prioridad;
....


class TareaspendientesAdmin extends AbstractAdmin
{
.....
        protected function configureFormFields(FormMapper $formMapper)
        {

         ....

                $formMapper
                                ....
                                ->add('prioridad', ModelType::class, [ 
                                        'query' => $queryBuilder 
                                ])
                               ......

with“-> add('prioridad',ModelType :: class [query'=> ...”我试图在Prioridad实体中列出prioridad字段。

对于$ queryBuilder变量,我尝试了以下选项:

//Option 1
$queryBuilder = $this->getModelManager()
           ->getEntityManager(Prioridad::class)
           ->createQueryBuilder('c')
           ->select('c.prioridad')
           ->from('App:Prioridad', 'c')
           ->orderBy('c.prioridad', 'ASC');

//Option 2
$queryBuilder = $this->getModelManager('
            SELECT s.prioridad 
              FROM App:Prioridad s
          ORDER BY s.prioridad');

//Option 3
$entity = new \App\Entity\Prioridad();
$queryBuilder=this->modelManager->getEntityManager($entity)->createQuery('SELECT t.prioridad FROM \App\Entity\Prioridad t ORDER BY t.prioridad ASC');

//Option 4
$em = $this->getModelManager()->getEntityManager(Prioridad::class);

        $queryBuilder = $em->createQueryBuilder()
                    ->select('p.prioridad')
                    ->from('App:Prioridad',  'p')
                    ->orderby('p.prioridad')
                    ->getQuery()
            ;   

//Option 5
$em = $this->getDoctrine()->getManager();
$queryBuilder = $em->getRepository('App:Tareaspendientes')->findPrioridades();

,但没有一个有效。在最后一个中,它给出以下错误:

Attempted to call an undefined method named "getDoctrine" of class "App\Admin\TareaspendientesAdmin

有什么想法吗?

0 个答案:

没有答案