Kdyby\Doctrine\EntityRepository::findAssoc PHP Méthode

findAssoc() public méthode

Fetches all records and returns an associative array indexed by key
public findAssoc ( array $criteria, string $key = NULL ) : array
$criteria array
$key string
Résultat array
    public function findAssoc($criteria, $key = NULL)
    {
        if (!is_array($criteria)) {
            $key = $criteria;
            $criteria = [];
        }
        $query = $this->createQueryBuilder('e')->whereCriteria($criteria)->resetDQLPart('from')->from($this->getEntityName(), 'e', 'e.' . $key)->getQuery();
        try {
            return $query->getResult();
        } catch (\Exception $e) {
            throw $this->handleException($e, $query);
        }
    }

Usage Example

 public function hashSongs()
 {
     /** @var Song[] $songs */
     $songs = $this->songRepository->findAssoc([]);
     /** @var \SplFileInfo $song */
     foreach (Finder::findFiles('*')->from($this->songsDirectory) as $song) {
         $songHash = md5_file($song->getRealPath());
         $songEntity = $songs[$song->getBasename('.mp3')];
         $songEntity->setHash($songHash);
     }
     $this->entityManager->flush();
 }