【发布时间】: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