user3282502
user3282502

Reputation: 33

Symfony2: How to override a specific class of a bundle

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

I need to override the DoctrineBuilder class in the Util Directory

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 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

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 }

Any Ideas ?

Thansk a lot!

Upvotes: 0

Views: 5147

Answers (2)

qooplmao
qooplmao

Reputation: 17759

For this you cannot directly override the builder class.

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

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

datatable.class: Acme\DatatableBundle\Util\Datatable

Upvotes: 0

Michael Sivolobov
Michael Sivolobov

Reputation: 13240

In normal (extendable) bundles you always have ability to override every part of it just by replacing one parameter with your value (default class name to your class name). But the bundle that you want to extend is not intended to be extended. So to extend it properly and override some parts you will need to do many manipulations.

In your particular case to use your own query builder you need to override __construct() of Datatable.php in your class and replace in it:

//from
$this->_queryBuilder = new DoctrineBuilder($container);
//to
$this->_queryBuilder = new YourDoctrineBuilder($container);

Also to make application to use your Datatable class you need to replace default class to yours in your parameters.yml:

datatable.class: Your\Path\To\Datatable

It is not necessary to implement the same directory structure in your bundle to override some parts of bundle. It has no effect! The only thing that you need is to define your own classes and set up bundle to use yours instead of original.

getParent() for bundle only works for cases when you need to override some Resources. They need to have the same structure as in original bundle. But your case is not about Resources.

Upvotes: 3

Related Questions