Created
October 2, 2013 18:32
-
-
Save hollodk/6798350 to your computer and use it in GitHub Desktop.
Symfony2 get from repository class
This file contains 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
Your controller: | |
/** | |
* @Route("/") | |
* @Template() | |
*/ | |
public function indexAction(Request $request) | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$user_id = 1234; | |
$products = $em->getRepository('AvenuePageBundle:Product')->getMyProducts($user_id); | |
Your Entity (make sure repositoryclass is defined in the annotations | |
<?php | |
namespace Avenue\PageBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* Product | |
* | |
* @ORM\Table() | |
* @ORM\Entity(repositoryClass="Avenue\PageBundle\Entity\ProductRepository") | |
*/ | |
class Product | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
Your repository class: | |
<?php | |
namespace Avenue\PageBundle\Entity; | |
use Doctrine\ORM\EntityRepository; | |
/** | |
* ProductRepository | |
* | |
* This class was generated by the Doctrine ORM. Add your own custom | |
* repository methods below. | |
*/ | |
class ProductRepository extends EntityRepository | |
{ | |
public function getMyProducts($user_id) | |
{ | |
return $this->createQueryBuilder('p') | |
->join('p.user', 'u') | |
->where('u.id = :user') | |
->setParameter('user', $user_id) | |
->getQuery() | |
->getResult(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment