0

我在多对多关系中有两个实体:

class Foo
{
    /**
     * @ORM\ManyToMany(targetEntity="Bar", inversedBy="foos")
     * @ORM\JoinTable(name="Foo_x_Bar")
     */
    protected $bars;
}

class Bar
{
    /**
     * @ORM\ManyToMany(targetEntity="Foo", mappedBy="bars")
     */
    protected $foos;
}

我想获取结果集中的所有 Foo 和 Bar 实例对,即:

array (size=2)
  0 => 
    array (size=2)
        'Foo' => Foo instance
        'Bar' => Bar instance
  1 => 
    array (size=2)
        'Foo' => Foo instance
        'Bar' => Bar instance

我尝试以网络上描述的几种方式进行操作,但我仍然无法选择整个实体。我可以使用此查询获取特定列:

SELECT f.something, b.somethingElse FROM Entity\Foo f LEFT JOIN f.bars b

但是当我在 SELECT 语句中省略列名时,我只会得到 Foo 实例并且 Bar 实例消失了。如何获得包含两个实体的结果集?

4

1 回答 1

0

The ManyToMany relation is really quite limited in Doctrine 2. It's all generated automatically and it's not an entity.

If you want to go beyond the basic default behavior then create yourself a FooBar entity and setup individual ManyToOne relations with Foo and Bar.

Doing so will allow you to query directly on FooBar. It also allows you to add additional properties/relations to your join tables.

于 2013-08-28T15:31:13.157 回答