2014-10-09 41 views
3

我是symfony 2的新手,真正喜欢它。有时候我不知道它是在文档中还是在我自己的文档中。symfony 2中的自定义存储库类无法正常工作

因为几个小时我试图让仓库类工作,并通过问题,并且还没有帮助我的教条文档。

因此,所有这些链接并不能帮助我

其实没有很多我应该做的,以实现正确的结果,但我总是得到一个错误:“未定义的方法'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)); 
    } 
} 

感谢您的阅读和帮助!

+0

您是否已在更改后清除了缓存? – qooplmao 2014-10-09 09:06:14

+0

@Qoop'rm -R〜/ Projects/Symfony/app/cache'? – caramba 2014-10-09 09:11:56

+0

或'app/console cache:clear' /'app/console cache:clear --env = prod | test | dev'取决于您的选择。 – qooplmao 2014-10-09 09:17:46

回答

3

感谢大家的帮助。我想通了,这是MYSELF当然没有什么错用symfony但有更多的我做了什么:

命令后:

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

我选择

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

而不是注释。所以我不得不改变我的

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

文件。<entity name="Acme\StoreBundle\Entity\Product">

<entity 
     name="Acme\StoreBundle\Entity\Product" 
     repository-class="Acme\StoreBundle\Entity\ProductRepository"> 
+0

是的,忘记告诉Doctrine使用您的存储库类是一个杀手。另一件事会让你眼花缭乱:如果你启用了缓存功能,请将其禁用。我花了几个小时学习了这个激情。 – David 2016-04-26 15:45:30

+0

同样适用于由yml模式生成的实体 – baldrs 2016-08-19 07:57:33

0

试试这个:

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

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

您也可以尝试不同的函数名。而不是findAllOrderedByName()选择例如findProducts()。

+0

谢谢。还是一样的错误。我也尝试过:'$ em = $ this-> getDoctrine() - > getManager();'但它没有改变任何东西 – caramba 2014-10-09 09:33:54

+0

我编辑了我的答案。 – repincln 2014-10-09 09:42:59

+0

再次感谢,我已经尝试过您的更新答案,仍然是相同的错误 – caramba 2014-10-09 09:46:35

相关问题