doctrine entityRepository如何工作?

时间:2017-08-04 14:47:01

标签: php symfony doctrine-orm orm

我是关于学说的新手,所以我尝试学习一些教程

ps:knpuniversity.com/screencast/doctrine-queries/dql

但我仍然对exp感到困惑:

src/AppBundle/Entity/Category.php

class Category
{

    private $id;

    private $name;

    private $iconKey;

    private $fortuneCookies;

我们可以通过

访问build in方法
$categoryRepository = $this->getDoctrine()
            ->getManager()
            ->getRepository('AppBundle:Category');
        $categories = $categoryRepository->findAll();

但是如果我们想要访问新方法,我们可以这样做

$categoryRepository = $this->getDoctrine()
            ->getManager()
            ->getRepository('AppBundle:Category');
        $categories = $categoryRepository->findAllOrdered();

然后提供存储库的类

的src /的appbundle /实体/ CategoryRepository.php

class CategoryRepository extends EntityRepository
{
    public function findAllOrdered()
    {
        die('this query will blow your mind...');
    }
}

我仍然没有得到它!

$categoryRepository = $this->getDoctrine()
            ->getManager()
            ->getRepository('AppBundle:Category');

如果上述内容指向类别实体,它怎么突然可以链接到CategoryRepository,我们可以访问findAllOrdered()方法?

我错过了一些有帮助的人吗?

2 个答案:

答案 0 :(得分:1)

正如一些开发人员告诉您的那样,通过注释将您的Entity类与Repository类链接起来非常重要。

  • @ORM\Table(name="your_entity_name")
  • @ORM\Entity(repositoryClass="Your_RepositoryClass")

如果您想使用更加自定义的名称(例如“repository”而不是“repository.category”)来呼叫您的AppBundle:Category,则可以通过yml文件创建服务。< / p>

答案 1 :(得分:1)

据我所知,您正在使用注释,并且很可能您有实体类的注释。您只需要像这样指定存储库类:

/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\CategoryRepository")
 * @ORM\Table(name="Category")
 */

指定