Created
November 28, 2012 19:32
-
-
Save ornj/4163511 to your computer and use it in GitHub Desktop.
Query for Doctrine entity that contains specified entity in a unidirectional ManyToMany relationship
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\TourBundle\Entity; | |
use Doctrine\ORM\EntityRepository; | |
use Doctrine\ORM\NoResultException; | |
/** | |
* TourRepository | |
*/ | |
class TourRepository extends EntityRepository { | |
/** | |
* Find all tours belonging to user that do not have the provided artwork | |
* | |
* @param User $user | |
* @param Author $author | |
* @return array | |
*/ | |
public function findByUserNotContainingArtwork($user, $artwork) { | |
$dql = 'SELECT t FROM AcmeTourBundle:Tour t LEFT JOIN t.user u LEFT JOIN t.artwork a WHERE u = :user AND NOT :artwork MEMBER OF t.artwork'; | |
$query = $this->getEntityManager()->createQuery($dql)->setParameter('user', $user)->setParameter('artwork', $artwork); | |
try { | |
return $query->getResult(); | |
} | |
catch (NoResultException $e) { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment