2
我有方法在我的仓库类,但它没有继承外键的回报:Symfony2。库法不返回额外的字段(外键)对于许多一对多
public function findAll()
{
$qb = $this->createQueryBuilder('d');
$result = $qb
->select('d', 'corporation', 'restaurant', 'deviceType', 'deviceAccess')
->leftJoin('d.corporation', 'corporation')
->leftJoin('d.restaurant', 'restaurant')
->leftJoin('d.deviceType', 'deviceType')
->leftJoin('d.accesses', 'deviceAccess')
->getQuery()
->getArrayResult();
return $result;
}
deviceAccess
这里是许多一对多的关系并且它也包含外键,但上面的方法不会返回这个外键。
我YAML配置:
AppBundle\Entity\Device:
type: entity
table: device
repositoryClass: AppBundle\Repository\DeviceRepository
manyToOne:
corporation:
targetEntity: Corporation
joinColumn:
onDelete: CASCADE
restaurant:
targetEntity: Restaurant
joinColumn:
onDelete: CASCADE
deviceType:
targetEntity: DeviceType
joinColumn:
onDelete: CASCADE
manyToMany:
accesses:
targetEntity: DeviceAccess
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
description:
type: text
nullable: true
和deviceAccess:
AppBundle\Entity\DeviceAccess:
type: entity
table: null
repositoryClass: AppBundle\Repository\DeviceAccessRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
manyToOne:
deviceAccessType:
targetEntity: DeviceAccessType
joinColumn:
onDelete: CASCADE
fields:
address:
type: string
length: 255
login:
type: string
length: 255
nullable: true
password:
type: string
length: 255
nullable: true
方法不返回deviceAccessType
场。 JSON结果 例子:
[
{
"id":34,
"description":"description1",
"corporation": {
"id":4,
"name":"corporation1",
"code":"74259",
"isDeleted":false
},
"restaurant": {
"id":5,
"name":"restaurant1",
"address":"",
"code":"1234",
"isDeleted":false,
"isBlacklist":false
},
"deviceType": {
"id":1,
"name":"printer",
"description":"printer"
},
"accesses":[
{
"id":22,
"address":"127.0.0.1",
"login":"admin",
"password":"password"
},
{
"id":23,
"address":"192.168.0.0",
"login":"user",
"password":"password"
}
]
}
]
如何解决这个问题,并迫使库方法返回所有FOREIGH钥匙?
非常感谢您的帮助!
只需要添加另外加入和更新的select语句: - > leftJoin(“deviceAccess.deviceAccessType”,'deviceAccessType)你也可以用预先加载鬼混,但加入是最直接的方式。 – Cerad
@Cerad非常感谢,这工作!请发表您的评论作为答案,我会接受它! –
Arf ....真遗憾。 – chalasr