2016-03-08 67 views
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鑰匙?

非常感謝您的幫助!

+2

只需要添加另外加入和更新的select語句: - > leftJoin(「deviceAccess.deviceAccessType」,'deviceAccessType)你也可以用預先加載鬼混,但加入是最直接的方式。 – Cerad

+0

@Cerad非常感謝,這工作!請發表您的評論作爲答案,我會接受它! –

+0

Arf ....真遺憾。 – chalasr

回答

2

下應該做的伎倆:

$result = $qb 
    ->select('d', 'corporation', 'restaurant', 'deviceType', 'deviceAccess', 'deviceAccessType') 
    // ... 
    ->leftJoin('d.accesses', 'deviceAccess') 
    ->leftJoin('deviceAccess.deviceAccessType', 'deviceAccessType') 
    ->getQuery() 
    ->getArrayResult(); 

return $result; 
相關問題