简体   繁体   English

手动编写DQL

[英]Manually writing DQL

I am trying this part in the Doctrine Documentation wherein you can: 我正在“ 教义文档”中尝试这一部分,您可以:


Manually writing DQL 手动编写DQL

For you SQL buffs, we didn't forget about you. 对于您来说,SQL迷们,我们没有忘记您。 You can optionally write your DQL queries manually and parse them in to a Doctrine_Query instance or just execute them. 您可以选择手动编写DQL查询,并将其解析为Doctrine_Query实例,或者仅执行它们。

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

Or you can just execute them by using the query() method of Doctrine_Query. 或者,您可以只使用Doctrine_Query的query()方法执行它们。

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

Yet I'm having difficulties since I've encountered the following error: 但是由于遇到以下错误,我遇到了困难:

Attempted to load class "Doctrine_Query" from namespace "AppBundle\\Controller". 尝试从命名空间“ AppBundle \\ Controller”中加载类“ Doctrine_Query”。 Did you forget a "use" statement for another namespace? 您是否忘记了另一个名称空间的“使用”语句?

Could you please help me with this? 你能帮我这个忙吗?

<?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));
    }

This is part of Doctrine 1.2 and not Doctrine 2.5 . 这是Doctrine 1.2的一部分,而不是Doctrine 2.5 In the latest version you would just create queries in the Doctrine Query Language with createQuery() . 在最新版本中,您只需使用createQuery()使用“ Doctrine查询语言”创建查询。

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

Alternatively you can write Native SQL . 或者,您可以编写本机SQL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM