Symfony2的。存储库方法不返回多对多的额外字段(外键)

时间:2016-03-08 16:39:39

标签: symfony doctrine many-to-many

我的Repository类中有方法,但它不返回继承的外键:

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"
      }
    ]
  }
]

如何解决此问题并强制Repository方法返回所有外围键?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

以下应该可以解决问题:

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

return $result;