我正在构建一个自定义存储库,我想根据一些关联进行选择 . 问题是我有一个单向关联,我不想做双向(有意),我选择了错误的方式 .

附:不想让它双向化的原因是因为 Bar (实际上是用户)被许多实体使用,我不想用所有那些反向关联阻塞我的 Bar 实体 .

例如:\ Entity \ Foo:

namespace Entity;
class Foo
{
    /**
     * @ManyToMany(targetEntity="\Entity\Bar")
     */
    $bars;

    /**
     * @Column(type="string")
     */
    $collection;
}

\实体\吧:

namespace Entity;
class Bar
{
}

\库\吧:

namespace Repository;
class Bar
{
    public function findByFooCollection($collection)
    {
        //something needs to go here
    }
}

我应该写sql,我会写这个查询:

SELECT
  *
FROM Bar
INNER JOIN Bar_Foo ON Bar_Foo.bar_id = Bar.id
INNER JOIN Foo on Foo.id = Bar_Foo.foo_id
WHERE Foo.collection = :collection

但是因为它是DQL或我正在使用的查询构建器,所以我被困在从 BarFoo 的关联中 .

虽然给我一个错误(逻辑上),这说明了我想做的事情 .

$this->getEntityManager()->createQueryBuilder()
                ->select('Bar')
                ->from('Entity\Foo', 'Foo')
                ->innerJoin('Foo.bars', 'Bar')
                ->where('Foo.collection = :collection')
                ->setParameter('collection', $collection);

是否可以从querybuilder或DQL返回 Bar 实体,在 Foo 的值上过滤而不必反转关联?