phpsymfonydoctrinefindby

Symfony findBy field name


I have association One Region ToMany Places. In PlacesRepository.php I have this code:

       $allPlacesArray = [];
       foreach ($placeTypes as $placeType){
           if($placeType == 'all'){
               $allPlacesArray = $this->findAll();
           }
           $allPlacesArray = array_merge($allPlacesArray, $this->findBy(['region' => 2]));
       }
       return $allPlacesArray;
   }

Now it searches places in region by id field. What I want is to search places in region by field name from Region entity, because from client I recieve name of region not id. I don't understand and can't find how I can do that. I've tried $this->findBy(['region.name' => 'Республика Крым'], but it doesn't work that way.


Solution

  • In case anyone needs an answer to this question. In repository:

    public function findAllPlacesInRegion() {
                $queryBuilder = $this->createQueryBuilder('p')
                    ->leftJoin('p.region', 'r')
                    ->where('r.name like :region_name' )
                    ->setParameter('region_name', 'Республика Крым');
                return $queryBuilder->getQuery()->getResult();
        }