【发布时间】:2014-10-09 08:46:12
【问题描述】:
我是 symfony 2 的新手,我真的很喜欢它。只是有时我不知道它是在文档中还是我自己。
从几个小时以来,我一直在尝试让存储库类工作,并且一直在解决问题以及所有对我没有帮助的学说文档。
所以所有这些链接都没有帮助我
- Symfony2s doctrine:generate:entities doesn't generate repo classes
- custom repository class in symfony2
- http://brentertainment.com/other/docs/book/doctrine/orm.html
实际上,我不需要做很多事情来完成正确的结果,但我总是收到一条错误消息:“未定义方法 'findAllOrderedByName'。方法名称必须以 findBy 或 findOneBy 开头!500 Internal Server错误 - BadMethodCallException”
我认为我的命名空间和/或使用语句有问题,但我不知道。谁能告诉我我做错了什么,也许还有我应该做些什么来做对?我想要的只是让findAllOrderedByName() 的方法起作用。
这就是我所拥有的:
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));
}
}
感谢阅读和帮助!
【问题讨论】:
-
进行更改后您是否清除了缓存?
-
@Qoop
rm -R ~/Projects/Symfony/app/cache? -
或
app/console cache:clear/app/console cache:clear --env=prod|test|dev取决于您的选择。 -
@Qoop 谢谢你,我刚刚做了,但还是同样的错误:(
-
这些想法怎么样? stackoverflow.com/a/15315838/1791606