Symfony2:如何覆盖包的特定类 [英] Symfony2: How to override a specific class of a bundle

查看:20
本文介绍了Symfony2:如何覆盖包的特定类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DatatableBundle 开发 symfony2 应用程序 (https://github.com/AliHichem/AliDatatableBundle)

I'm developping a symfony2 application with the DatatableBundle (https://github.com/AliHichem/AliDatatableBundle)

我需要覆盖Util目录中的DoctrineBuilder类

I need to override the DoctrineBuilder class in the Util Directory

我在我的 src 目录中创建了一个 DtatableBundle,其中包含 Alidatatable Bundle 的所有结构我在 newBundle 中编写了一个 getParentMethod,并创建了我的新 DoctrineBuilde 类

I've created a DtatableBundle in my src directory with all the structure of the Alidatatable Bundle I've write a getParentMethod in the newBundle, and create my new DoctrineBuilde class

Symfony 总是使用 vendor 类而不是新的

Symfony always use the vendor class and not the new one

这里是捆绑结构,然后是我想要覆盖的类:

Here,s the Bundle Structure and then class i want to override :

DatatableBundle  
 Util
   Factory
     Query
       DoctrineBuilder.php

和 bundle 的服务定义:参数:datatable.class: Ali\DatatableBundle\Util\Datatable

and the bundle's service definition: parameters: datatable.class: Ali\DatatableBundle\Util\Datatable

services:
    datatable:
        class: "%datatable.class%"
        arguments: [ @service_container ]
        scope: prototype

    datatable.twig.extension:
        class: Ali\DatatableBundle\Twig\Extension\AliDatatableExtension
        arguments: [ @service_container ]
        tags:
            -  { name: twig.extension }

有什么想法吗?

非常感谢!

推荐答案

为此,您不能直接覆盖构建器类.

For this you cannot directly override the builder class.

但是,您可以像这样覆盖 Datatable 类并在 __construct 中调用您自己的构建器版本..

You could, however, override the Datatable class and call your own version of the builder in the __construct like so..

namespace Acme\DatatableBundle\Util;

use Ali\DatatableBundle\Util\Datatable as BaseDatatable;
use Acme\DatatableBundle\Util\Factory\Query\DoctrineBuilder;

class Datatable extends BaseDatatable
{
    /**
     * class constructor 
     * 
     * @param ContainerInterface $container 
     */
    public function __construct(ContainerInterface $container)
    {
        parent::__construct($container);

        // This would change the default for your version of the builder
        $this->_queryBuilder = new DoctrineBuilder($container);
    }
}

然后只需将您的数据表类版本设置为 %datatable.class% 参数,例如..

And then just set your version of the datatable class as the %datatable.class% parameter like..

datatable.class: Acme\DatatableBundle\Util\Datatable

这篇关于Symfony2:如何覆盖包的特定类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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