Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 1.01 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Doctrine - Get row or rows from many results based on column value
  2. $em = $this->getDoctrine()->getManager();
  3.  
  4. $query = $em->createQuery(
  5.    'SELECT u, p FROM MYUserBundle:User u
  6.    JOIN u.post p'
  7. );
  8.        
  9. foreach($query->getResult() as $user){
  10.  
  11.    //a bunch of posts related to this user
  12.    $posts = $user->getPosts();
  13. }
  14.        
  15. $posts = $user->getPosts();
  16. $post = $posts->findBySlug('my_slug');
  17.  
  18. //or something along those lines...
  19.        
  20. public function getPostsBySlug( $slug )
  21. {
  22.   return $this->posts->filter( function( Post $post ) use ( $slug )
  23.   {
  24.     return $post->getSlug() == $slug;
  25.   } );
  26. }
  27.        
  28. $posts = $user->getPostsBySlug( 'my_slug' );
  29.        
  30. $slug = 'my_slug';
  31. array_filter($user->getPosts()->all(), function ($post) use ($slug) {
  32.     return $post->getSlug() == $slug;
  33. })
  34.        
  35. namespace AcmeThingBundleRepository;
  36.  
  37. use DoctrineORMEntityRepository;
  38.  
  39. class PostRepository extends EntityRepository
  40. {
  41.     public function getBySlug($slug)
  42.     {
  43.         /// your custom query here
  44.  
  45.         return $q->getQuery()->getResult();
  46.     }
  47. }