Php symfony2中的自定义存储库类

Php symfony2中的自定义存储库类,php,symfony,Php,Symfony,我是symfony2新手。我在通过命令行创建实体类时创建了一个存储库类。但我无法访问该存储库类中的自定义函数。如何在symfony2中创建自定义存储库类?有谁能用一些示例代码从头开始一步一步地给我解释一下吗 下面是我的存储库类 namespace Mypro\symBundle\Entity; use Doctrine\ORM\EntityRepository; /** * RegisterRepository * This class was generated by the Doct

我是symfony2新手。我在通过命令行创建实体类时创建了一个存储库类。但我无法访问该存储库类中的自定义函数。如何在symfony2中创建自定义存储库类?有谁能用一些示例代码从头开始一步一步地给我解释一下吗

下面是我的存储库类

namespace Mypro\symBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * RegisterRepository
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class RegisterRepository extends EntityRepository
{


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


}
我这样叫我的控制器

$em = $this->getDoctrine()->getEntityManager();
          $pro = $em->getRepository('symBundle:Register')
            ->findAllOrderedByName();
我得到了下面的错误

Undefined method 'findAllOrderedByName'. The method name must start with either findBy or findOneBy!

我的代码有错误吗?创建存储库类时有错误吗?我是否需要使用任何类。

我想您只是忘了在您的实体中注册此存储库。 您只需在实体配置文件中添加repository类

在src/Mypro/symBundle/Resources/config/doctor/Register.orm.yml中:

Mypro\symBundle\Entity\Register:
    type: entity
    repositoryClass: Mypro\symBundle\Entity\RegisterRepository
在进行此更改后,不要忘记清除缓存,以防万一

如果您使用的是注释(而不是yml config),那么可以添加如下内容,而不是上述内容:

/**
 * @ORM\Entity(repositoryClass="Mypro\symBundle\Entity\RegisterRepository")
*/

到您的实体类注册存储库

手册有一个很好的分步指南

  • 首先在annotations/yaml配置中定义repository类
  • 创建类
  • 创建函数
  • 然后调用新创建的函数

首先,您不需要定制回购来实现这一点。。您可以在EM getRepository findBy方法中设置order by子句:

//$em - entity manager
//from Doctrine documentation: findBy(criteria(array), order(array), limit, offset)
$result = $em->getRepository('symBundle:Register')->findBy(array(), array('name' => 'ASC'))

当我陷入这种混乱的配置时,我也浪费了很多时间。我将实体中的存储库类配置为注释映射。映射已经存在,但存储库仍然没有与实体关联。当我移动注释映射时,即。 @ORM\Entity(repositoryClass=“Acme\DemoBundle\Entity\UserRepository”),到最后一行,它起作用了

 /*
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\UserRepository")
 */
class User
{
...
}`   

我只需遵循本机文档并查找我选择的注释。例如,我选择了yaml,向Product.orm.yml和Category.orm.yml文件添加了配置,还向实体方法添加了新的php属性:

protected $products;

public function __construct()
{
    $this->products = new ArrayCollection();
}
对于Category.php和

protected $category;
对于Product.php

然后运行
php应用程序/控制台原则:generate:entities-Acme
,成功添加新的getter和setter以进行映射: 更新文件夹Resource/config/doctor中的xml映射文件,添加存储库类属性:

<entity name="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContent" table="uv_update_page_content" repository-class="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContentRepository">

Reuven我得到了正确的输出,但在我的Netbeans中,ide无法检测到我的自定义存储库函数。y?你知道吗?我认为netbean不可能知道自定义存储库。我的netbean版本没有自动完成特定于symfony的代码。我也有同样的问题,我确实用symfony生成了EntityRepository。。。但我在src/Projet/Bundle/Resources/config/中没有任何名为“条令”的文件夹。。。怎么了?我也是。我也没有条令目录。。我必须先创建它,然后再创建提到的文件吗?这是yml实体配置的语法。有关注释,请参阅条令文档。manaul中的示例不适用于Symfony 2.1.1,除非您将注释从
@ORM\Entity(repositoryClass=“Acme\StoreBundle\Repository\ProductRepository”)
更改为类似
@ORM\Entity(repositoryClass=“Acme\StoreBundle\Entity\ProductRepository”)的内容
这是从Entity\Product.php文件的名称空间开始的,在我花了2个小时试图找出问题所在后,这也适用于我。
php app/console doctrine:cache:clear-metadata
php app/console cache:clear