I'm new to symfony 2 and actually love it. Just sometimes I don't know if it is in the docs or just my self.
Since hours I try to get the repository class to work and been trough the questions and also the doctrine docs which alle didn't help me.
So all those links didn't help me
- Symfony2s doctrine:generate:entities doesn't generate repo classes
- custom repository class in symfony2
- http://brentertainment.com/other/docs/book/doctrine/orm.html
Actually there isn't a lot I should do to accomplish the right result but I always get an error saying: "Undefined method 'findAllOrderedByName'. The method name must start with either findBy or findOneBy! 500 Internal Server Error - BadMethodCallException"
I think there is something wrong with my namespaces and or use statements, but I have no idea. Can anyone tell me what I'm doing wrong and maybe also what I should do to get it right? All I want is to get that method findAllOrderedByName()
to work.
so here is what I have:
Symfony/src/Acme/StoreBundle/Entity/Product.php
<?php
namespace Acme\StoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="Acme\StoreBundle\Entity\ProductRepository")
*/
class Product
{
// all the code which was/is working
}
Symfony/src/Acme/StoreBundle/Entity/ProductRepository.php
<?php
namespace Acme\StoreBundle\Entity;
use Doctrine\ORM\EntityRepository;
class ProductRepository extends EntityRepository
{
public function findAllOrderedByName()
{
return $this->getManager()
->createQuery(
'SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC'
)
->getResult();
}
}
Symfony/src/Acme/StoreBundle/Controller/DefaultController.php
<?php
namespace Acme\StoreBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\StoreBundle\Entity\Product;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
public function showAction()
{
$em = $this->get('doctrine')->getManager();
$products = $em->getRepository('AcmeStoreBundle:Product')
->findAllOrderedByName();
return $this->render('AcmeStoreBundle:Default:showAll.html.twig', array('products' => $products));
}
}
Thanks for reading and helping!