spacecodeur
spacecodeur

Reputation: 2406

Symfony 4, provide simple example for a findby custom method in ManyToMany relation context

I have two simple entities A and B : (entity 'AB' automatically created by symfony 4)

enter image description here

With theses examples datas :

enter image description here

In entry, I have only the names of one entitie B (by example 'foo') and the name of one entity A ( by example 'A_b')

From a Repository class ( A repository class or B Repository class ? ), how can I get the entity B depending of only the strings 'foo' and 'A_b' entries ?

I must obtains in result the entity B with the id 1

enter image description here

I'm not a very strong doctrine DQL user.. And I'm looking for a proper way

Upvotes: 0

Views: 92

Answers (1)

Themer
Themer

Reputation: 659

You can try this code:

 public function getBByName($name)
 {
    $qb = $this->createQueryBuilder('A');
    $qb->leftJoin('A.b', 'B');
    $qb->where('B.name= :name');
    $qb->setParameter('name', $name);

    $query = $qb->getQuery();
    $result = $query->getResult();
    return $result;
  }

Upvotes: 1

Related Questions