Skip to content

Instantly share code, notes, and snippets.

@ornj
Created November 28, 2012 19:32
Show Gist options
  • Save ornj/4163511 to your computer and use it in GitHub Desktop.
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
<?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