I am building a custom repository, and i want to make a selection based on some associations. The problem is that i have a unidirectional association that i do not want to make bidirectional (intentional) and i am selecting the wrong way.
P.S. The reason for not wanting to make it bidirectional is because Bar
(in reality user) is used by a lot of entities and i do not want to clog my Bar
entity with all those inversed associations.
for example: \Entity\Foo:
namespace Entity;
class Foo
{
/**
* @ManyToMany(targetEntity="\Entity\Bar")
*/
$bars;
/**
* @Column(type="string")
*/
$collection;
}
\Entity\Bar:
namespace Entity;
class Bar
{
}
\Repository\Bar:
namespace Repository;
class Bar
{
public function findByFooCollection($collection)
{
//something needs to go here
}
}
Should i be writing sql, i would write this query:
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
However as it is DQL or the querybuilder i am using, I get stuck at the association from Bar
to Foo
.
While giving me an error (logically), this illustrates what i want to do.
$this->getEntityManager()->createQueryBuilder()
->select('Bar')
->from('Entity\Foo', 'Foo')
->innerJoin('Foo.bars', 'Bar')
->where('Foo.collection = :collection')
->setParameter('collection', $collection);
Is it possible to return a Bar
entity from a querybuilder or DQL, filtered on a value of Foo
without having to inverse the association?