Ver Mensaje Individual
  #1 (permalink)  
Antiguo 10/06/2016, 03:45
hamramr
 
Fecha de Ingreso: marzo-2007
Mensajes: 59
Antigüedad: 17 años, 1 mes
Puntos: 0
Pregunta Custom Repository Classes

Buenos días.
Tengo un controlador: /src/IP/RestBundle/Controller/DefaultController.php
Tengo una entidad: /src/IP/RestBundle/Entity/Productos.php
Tengo un repository de la entidad: /src/IP/RestBundle/Entity/ProductosRepository.php

En el DefaultController.php quiero usar una función definida en el ProductosRepository.php para obtener todos los productos en base a unos parámetros.

En Productos.php tengo:

Código:
<?php

namespace IP\RestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Productos
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="IP\RestBundle\Entity\ProductosRepository")
 */
class Productos
{
...

En ProductosRepository.php tengo:
Código:
<?php

namespace IP\RestBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * ProductosRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ProductosRepository extends EntityRepository
{
	public function findAllProductos()
	{
		$em = $this->get('doctrine')->getManager();

        $dql = 'SELECT p FROM IPRestBundle:Productos p';

        return $em->createQuery($dql)
            ->setFirstResult(0)
            ->setMaxResults(2)
            ->getResult();
	}

}
Finalmente, dentro del DefaultController.php tengo esto:

Código:
$em = $this->getDoctrine()->getManager();

        $productos = $em->getRepository('IPRestBundle:Productos')->findAllProductos();
        print_r($productos);
        exit();
Al probarlo me devuelve esto:
Código:
Undefined method 'get'. The method name must start with either findBy or findOneBy!
He definido todo esto siguiendo lo que indican en la página de Symfony:
http://symfony.com/doc/2.7/book/doct...sitory-classes

He borrado la cache, la cache de doctrine, etc, pero nada de nada.
¿Alguien sabe por qué pasa esto por favor?

Muchas gracias.