手动编写 DQL

Manually writing DQL

我正在 Doctrine Documentation 中尝试这部分,您可以:


手动编写DQL

对于你们 SQL 爱好者,我们没有忘记你们。您可以选择手动编写 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 and not Doctrine 2.5. In the latest version you would just create queries in the Doctrine Query LanguagecreateQuery() 的一部分。

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

或者您可以写成Native SQL