I have 2 entities defined:
- State entity
- City entity
I am trying to select the state entity and automatically join the city entity and order the results by state's name. However, I get this error:
[Syntax Error] line 0, col 74: Error: Expected Literal, got 'BY'
My code to create the query is:
$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();
If I remove the order By method I get the following error:
[Syntax Error] line 0, col -1: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got end of string.
The DQL generated queries ( with order By and 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
The state entity:
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: { }
And the city entity:
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.nameand you don't definesin your query. - undefined