【问题标题】:symfony2 & doctrine orderBy/DQL errorsymfony2 & 学说 orderBy/DQL 错误
【发布时间】:2014-07-27 16:03:26
【问题描述】:

我定义了 2 个实体:

  • 国家实体
  • 城市实体

我正在尝试选择州实体并自动加入城市实体并按州名称对结果进行排序。但是,我收到此错误:

[Syntax Error] line 0, col 74: Error: Expected Literal, got 'BY' 

我创建查询的代码是:

$statesQuery = $this->getEm()->createQueryBuilder();
$statesQuery->select("st");
$statesQuery->from("CoreBundle:State", "st");
$statesQuery->leftJoin("CoreBundle:City", "ct");
$statesQuery->addSelect("ct");
$statesQuery->orderBy("st.name", "ASC");

$statesQuery = $statesQuery->getQuery();

$states = $statesQuery->getResult();

如果我通过方法删除订单,我会收到以下错误:

[Syntax Error] line 0, col -1: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got end of string. 

DQL 生成的查询(带有 order By 和 w/o orderBy):

SELECT st, ct FROM CoreBundle:State st LEFT JOIN CoreBundle:City ct ORDER BY s.name ASC

SELECT st, ct FROM CoreBundle:State st LEFT JOIN CoreBundle:City ct

国家实体:

Ec\CoreBundle\Entity\State:
    type: entity
    table: states
    id:
        id:
            type: integer
            nullable: false
            unsigned: false
            comment: ''
            id: true
            generator:
                strategy: IDENTITY
    fields:
        name:
            type: string
            nullable: false
            length: 255
        friendly_url:
            type: string
            nullable: false
            length: 255
    oneToMany:
        cities:
            targetEntity: City
            mappedBy: state
    oneToOne:
        country:
            targetEntity: Country
    lifecycleCallbacks: {  }

以及城市实体:

Ec\CoreBundle\Entity\City:
    type: entity
    table: cities
    id:
        id:
            type: integer
            nullable: false
            unsigned: false
            comment: ''
            id: true
            generator:
                strategy: IDENTITY
    fields:
        name:
            type: string
            nullable: false
            length: 255
        friendly_url:
            type: string
            nullable: false
            length: 255
    manyToOne:
        state:
            targetEntity: State
            inversedBy: cities
    lifecycleCallbacks: {  }

【问题讨论】:

  • 您通过s.name 订购,但您没有在查询中定义s
  • 我在这里发布这个问题之前做了很多试验和错误,“s.name”可能会滑倒。我已经更正了我的问题。

标签: php symfony doctrine-orm doctrine dql


【解决方案1】:

如果你想检索一个州的集合(和相关的城市),这就足够了:

// src/AcmeBundle/Controller/MyController.php
// ...

$statesQuery = $this->getDoctrine()->getManager()->createQueryBuilder();
$statesQuery->select("st")
            ->from("CoreBundle:State", "st")
            ->orderBy("st.name", "ASC");

$states = $statesQuery->getQuery()
                      ->getResult();

所以城市将被延迟加载。有关延迟/急切加载的更多信息,请查看doctrine2 doc

编辑: 好的,如果您只在一个地方需要它,只需执行以下操作:

$statesQuery = $this->getDoctrine()->getManager()->createQueryBuilder();
$statesQuery->select("st, ct")
            ->from("CoreBundle:State", "st")
            ->leftJoin("st.cities", "ct")
            ->orderBy("st.name", "ASC");

$states = $statesQuery->getQuery()
                      ->getResult();

【讨论】:

  • 不幸的是,我正在显示所有州及其城市的列表,如果我以惰性模式加载城市,则会产生 54 个查询。这是应用程序中唯一需要急切加载城市的页面,因此我无法在实体定义中将获取类型设置为急切。国家实体只是为了支持将来对额外国家的轻松实施,此时我只将一个国家添加到我的数据库中。我可以确认,如果我不急切地加载城市,则 order by 语句有效。
  • 您好,我编辑了我的答案,现在您可以看到如何急切地获取数据。
  • @raullarion 如果答案解决了您的问题,请勾选为已接受
  • so ->select("st", "ct") 实际上是问题解决者。我没看到 :-) 谢谢!
猜你喜欢
  • 2012-03-11
  • 1970-01-01
  • 2012-10-08
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多