3

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

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!

Community
  • 1
  • 1
caramba
  • 21,963
  • 19
  • 86
  • 127

2 Answers2

3

Thanks for everyone who was helping. I figured it out, IT WAS MYSELF of course there was nothing wrong with symfony but much more with what I did:

After the command:

 php app/console generate:bundle --namespace=Acme/StoreBundle

I choose for

"Determine the format to use for the generated configuration." --> xml

And not annotation. So I had to change in my

~/Projects/Symfony/src/Acme/StoreBundle/Resources/config/doctrine/Product.orm.xml

file. the <entity name="Acme\StoreBundle\Entity\Product"> to

<entity
        name="Acme\StoreBundle\Entity\Product"
        repository-class="Acme\StoreBundle\Entity\ProductRepository">
caramba
  • 21,963
  • 19
  • 86
  • 127
  • Yes, forgetting to tell Doctrine to use your repository class is a killer. Another thing that will make you tear your eyeballs out: if you have caching enabled, disable it. I just spent a couple hours learning that lession. – David Apr 26 '16 at 15:45
  • Same goes for entities generated out of yml schema – baldrs Aug 19 '16 at 07:57
0

Try this:

$products = $this->getDoctrine()
        ->getRepository('AcmeStoreBundle:Product')
        ->findAllOrderedByName();

and

public function findAllOrderedByName(){
    return $this->getEntityManager()
        ->createQuery(
        'SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC'
        )
        ->getResult();
}

You can also try different function name. Instead of findAllOrderedByName() choose for example findProducts().

repincln
  • 2,029
  • 5
  • 24
  • 34
  • Thank you. Still the same error. I've also tried with: `$em = $this->getDoctrine()->getManager();` but it doesn't changes anything – caramba Oct 09 '14 at 09:33
  • Thanks again, I've tried with your updated answer, still the same error – caramba Oct 09 '14 at 09:46