src/Sulu/Bundle/MediaBundle/Entity/MediaRepository.php
public function findMediaWithFilenameInCollectionWithId($filename, $collectionId)
    {
        $queryBuilder = $this->createQueryBuilder('media')
            ->innerJoin('media.files', 'files')
            ->innerJoin('files.fileVersions', 'versions', 'WITH', 'versions.version = files.version')
            ->join('media.collection', 'collection')
            ->where('collection.id = :collectionId')
            ->andWhere('versions.name = :filename')
            ->orderBy('versions.created')
            ->setMaxResults(1)
            ->setParameter('filename', $filename)
            ->setParameter('collectionId', $collectionId);
        $result = $queryBuilder->getQuery()->getResult();

        if (count($result) > 0) {
            return $result[0];
        }

        return;
    }
public function findMediaWithFilenameInCollectionWithId($filename, $collectionId)
    {
        $queryBuilder = $this->createQueryBuilder('media')
            ->innerJoin('media.files', 'files')
            ->innerJoin('files.fileVersions', 'versions', 'WITH', 'versions.version = files.version')
            ->join('media.collection', 'collection')
            ->where('collection.id = :collectionId')
            ->andWhere('versions.name = :filename')
            ->orderBy('versions.created')
            ->setMaxResults(1)
            ->setParameter('filename', $filename)
            ->setParameter('collectionId', $collectionId);
        $result = $queryBuilder->getQuery()->getResult();

        if (count($result) > 0) {
            return $result[0];
        }

        return;
    }
public function findMediaWithFilenameInCollectionWithId ( $ filename , $ collectionId ) { $ queryBuilder = $ this -> createQueryBuilder ( 'media' ) -> innerJoin ( 'media.files' , 'files' ) -> innerJoin ( 'files.fileVersions' , 'versions' , 'WITH' , 'versions.version = files.version' ) -> join ( 'media.collection' , 'collection' ) -> where ( 'collection.id = :collectionId' ) -> andWhere ( 'versions.name = :filename' ) -> orderBy ( 'versions.created' ) -> setMaxResults ( 1 ) -> setParameter ( 'filename' , $ filename ) -> setParameter ( 'collectionId' , $ collectionId ) ; $ result = $ queryBuilder -> getQuery ( ) -> getResult ( ) ; if ( count ( $ result ) > 0 ) { return $ result [ 0 ] ; } return ; }
Returns the most recent version of a media for the specified
filename within a collection.

@param string $filename
@param int $collectionId

@return Media
Returns the most recent version of a media for the specified filename within a collection .