php – 手动编写DQL

我在 Doctrine Documentation中尝试这个部分,你可以:

手动编写DQL

对于你的SQL buff,我们没有忘记你.您可以选择手动编写DQL查询并将其解析为Doctrine_Query实例或只执行它们.

$dql = "FROM User u,u.Phonenumbers p";
$q = Doctrine_Query::create()->parseQuery($dql);

或者您可以使用Doctrine_Query的query()方法执行它们.

$dql = "FROM User u,u.Phonenumbers p";
$q = Doctrine_Query::create()->query($dql);

然而,由于遇到以下错误,我遇到了困难:

Attempted to load class “Doctrine_Query” from namespace “AppBundle\Controller”.
Did you forget a “use” statement for another namespace?

你能帮帮我吗?

<?php

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\TblProduct;

class DefaultController extends Controller
{
    /**
     * @Route("/",name="homepage")
     */
    public function indexAction()
    {
        $products = "SELECT * FROM TblProduct";
        $q = Doctrine_Query::create()->query($products);

        if (!$products) {
            throw $this->createNotFoundException(
                'No products registered yet.'
            );
        }
        return $this->render('default/index.html.twig',array('products' => $products));
    }

解决方法

这是 Doctrine 1.2而不是 Doctrine 2.5的一部分.在最新版本中,您只需使用createQuery()在 Doctrine Query Language中创建查询.

<?php
$dql = "FROM User u,u.Phonenumbers p";
$query = $em->createQuery($dql);
$users = $query->getResult();

或者你可以写Native SQL.

相关文章

文章浏览阅读8.4k次,点赞8次,收藏7次。SourceCodester Onl...
文章浏览阅读3.4k次,点赞46次,收藏51次。本文为大家介绍在...
文章浏览阅读1.1k次。- php是最优秀, 最原生的模板语言, 替代...
文章浏览阅读1.1k次,点赞18次,收藏15次。整理K8s网络相关笔...
文章浏览阅读1.2k次,点赞22次,收藏19次。此网络模型提供了...
文章浏览阅读1.1k次,点赞14次,收藏19次。当我们谈论网络安...