我是symfony 2的新手,真正喜欢它。有时候我不知道它是在文档中还是在我自己的文档中。symfony 2中的自定义存储库类无法正常工作
因为几个小时我试图让仓库类工作,并通过问题,并且还没有帮助我的教条文档。
因此,所有这些链接并不能帮助我
- Symfony2s doctrine:generate:entities doesn't generate repo classes
- custom repository class in symfony2
-
其实没有很多我应该做的,以实现正确的结果,但我总是得到一个错误:“未定义的方法'findAllOrderedByName'。方法名称必须以findBy或findOneBy! 500内部服务器错误 - BadMethodCallException“
我认为我的命名空间和/或使用语句有问题,但我不知道。谁能告诉我我做错了什么,也许我应该做什么?得到它的权利我要的是让该方法findAllOrderedByName()
工作
所以这里就是我:
的Symfony/src目录/阿克米/ StoreBundle /实体/ 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目录/阿克米/ StoreBundle /控制器/ 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));
}
}
感谢您的阅读和帮助!
您是否已在更改后清除了缓存? – qooplmao 2014-10-09 09:06:14
@Qoop'rm -R〜/ Projects/Symfony/app/cache'? – caramba 2014-10-09 09:11:56
或'app/console cache:clear' /'app/console cache:clear --env = prod | test | dev'取决于您的选择。 – qooplmao 2014-10-09 09:17:46