symfony 2中的自定义存储库类不工作



我是symfony 2的新手,真的很喜欢它。只是有时我不知道它是在文档中还是我自己。

几个小时以来,我一直在努力让知识库类开始工作,并一直在解决问题和所有人都没有帮助我的理论文档。

所以所有这些链接对我没有帮助

  • Symfony2学说:生成:实体不会;t生成回购类别
  • symfony2中的自定义存储库类
  • http://brentertainment.com/other/docs/book/doctrine/orm.html

事实上,我不需要做很多事情来实现正确的结果,但我总是收到一个错误,说:"未定义的方法'findAllOrderedByName'。方法名称必须以findBy或findOneBy开头!500内部服务器错误-BadMethodCallException"

我认为我的名称空间和/或use语句有问题,但我不知道。有人能告诉我我做错了什么吗?也许还有我应该做些什么来纠正错误?我只想让findAllOrderedByName()方法发挥作用。

这就是我所拥有的:

Symfony/src/Acme/StoreBundle/Entity/Product.php

<?php
namespace AcmeStoreBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * @ORMEntity(repositoryClass="AcmeStoreBundleEntityProductRepository")
 */
class Product
{ 
    // all the code which was/is working
}

Symfony/src/Acme/StoreBundle/Entity/ProductRepository.php

<?php
namespace AcmeStoreBundleEntity;
use DoctrineORMEntityRepository;
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 AcmeStoreBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use AcmeStoreBundleEntityProduct;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpFoundationRequest;
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));
    }
}

感谢您的阅读和帮助!

感谢所有提供帮助的人。我发现了,这是我自己当然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="AcmeStoreBundleEntityProduct">

<entity
        name="AcmeStoreBundleEntityProduct"
        repository-class="AcmeStoreBundleEntityProductRepository">

试试这个:

$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();
}

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

最新更新