Reputation: 22490
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!
Upvotes: 3
Views: 4146
Reputation: 22490
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">
Upvotes: 3
Reputation: 2059
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().
Upvotes: 0