Skip to content

Instantly share code, notes, and snippets.

@hollodk
Created October 2, 2013 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hollodk/6798350 to your computer and use it in GitHub Desktop.
Save hollodk/6798350 to your computer and use it in GitHub Desktop.
Symfony2 get from repository class
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