如何在Symfony2中扩展EntityRepository? [英] How to extend EntityRepository in Symfony2?

查看:295
本文介绍了如何在Symfony2中扩展EntityRepository?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数getAllOrderedBy()这个方法在所有的Repository中都是重复的, ($ column){

$ qb = $ this-> createQueryBuilder('ac')
- > select('ac')
- > orderBy ac。'。$ column);

return $ qb-> getQuery() - > getResult();
}

我想在另一个超类中提取它,例如OrderedRepository,并将其用作所有其他存储库的基类。



现在问题是如何做到这一点?



我试过在OrderedRepository的构造函数中实例化EntityRepository,
这样的东西,但也实例化所有内部对象,其他东西需要,但它没有真正的工作,我觉得这是错误的路径跟随。

  function __construct(){

parent :: __ construct();
$ this-> blabla_option =实例化;
}

可以请举一个正确扩展EntityRepository的例子,而不是这个扩展类可以作为其他存储库的基类?



PS我是PHP的创始人,所以请原谅我,如果我用我的不知情伤害你的感觉。

解决方案

这更多是一个Doctrine2



假设您正在使用注释作为您的教义映射,则必须在实体中声明您正在使用哪个存储库类:

  / ** 
* @ ORM\Entity(repositoryClass =Fully\Qualified\Namespace\To\MyRepository)
* /
class MyEntity {}

如下所述: http://symfony.com/doc/2.0/book/doctrine.html#custom-repository-classes



然后,您可以使用标准类继承来编写此自定义MyRepository类。



您可以想象类似的东西:

  class OrderedRepository扩展EntityRepository 
{
//一些额外的方法...
}

class MyRepository extends OrderedRespository {}

最后,如果你想覆盖你的__constructor存储库,您以相同的参数初始化父构造函数:

  public function __construct($ em,Mapping\ClassMetadata $ class)
{
parent :: __ construct($ em,$ class);
//一些额外的东西
}


I got this problem I have a method that is repetitive in all the Repositories, for example this method.

function getAllOrderedBy($column) {

    $qb = $this->createQueryBuilder('ac')
            ->select('ac')
            ->orderBy('ac.' . $column);

    return $qb->getQuery()->getResult();
}

I want to extract it in another superclass, OrderedRepository for example and use it as the base class for all the other repositories.

Now the problem is how to do that ?

I tried to instantiate EntityRepository in the constructor of the OrderedRepository, something like this, but also instantiating there all the internal objects, needed for other stuff, but it didn't really worked, and I felt it is the wrong path to follow.

function __construct() {

  parent::__construct();
  $this->blabla_option = "instantiated";
}

Could you please give an example of correct extending of EntityRepository so than this extended class could serve as a base class for other repositories ?

P.S. I'm a begginer in PHP so please excuse me if I hurt your feelings with my unawareness.

解决方案

This is more a Doctrine2 thing.

Assuming you are using annotations for your doctrine mapping, you have to declare which repository class you are using in the Entity:

/**
 * @ORM\Entity(repositoryClass="Fully\Qualified\Namespace\To\MyRepository")
 */
class MyEntity { }

as explained here: http://symfony.com/doc/2.0/book/doctrine.html#custom-repository-classes .

Then, you can code this custom MyRepository class, using standard class inheritance.

You could imagine something like that:

class OrderedRepository extends EntityRepository 
{
    //  some extra methods...
}

class MyRepository extends OrderedRespository {}

Finally, if you want to override the __constructor of your repository, you have to initailize the parent constructor with the same arguments:

public function __construct($em, Mapping\ClassMetadata $class)
{
    parent::__construct($em, $class);
    // some extra stuff
}

这篇关于如何在Symfony2中扩展EntityRepository?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆