-->

SonataDoctrineORM - Model extends

2019-05-31 03:22发布

问题:

I'm using Sonata with SonataAdmin & SonataOrm as told in several tutorials.

I simply would like to remove some default method of DoctrineOrmBundle- ModelManager.php

I tried to override the ModelManager by putting

<?php

namespace Project\AdminBundle\Model;

use Sonata\DoctrineORMAdminBundle\Model\ModelManager as ModelManager;

class ModelManager extends ModelManager
{

/**
 * {@inheritdoc}
 */
public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid)
{
    $values = $datagrid->getValues();
    $values = $_GET['filter'];
    if ($fieldDescription->getName() == $values['_sort_by']) {
        //echo $fieldDescription->getName() . ' --- ' . $values['_sort_order'] . '<br />';
        if ($values['_sort_order'] == 'ASC') {
            $values['_sort_order'] = 'DESC';
        } else {
            $values['_sort_order'] = 'ASC';
        }
    } else {
        $values['_sort_order'] = 'ASC';
        $values['_sort_by']    = $fieldDescription->getName();
    }

    return array('filter' => $values);
  }

}
?>

And tell Sonata DoctrineOrm to use it by default.

But I don't know how to do it.

Am I at least on the right track ?

回答1:

You still need to tell the adminbundle to use your custom ModelManager. To do so you have to apply the setModelManager method when defining your admin services. Services.yml:

services:
    #new model manager
    myproject.model_manager:
         class: Project\AdminBundle\Model\ModelManager
         arguments:
             - '@doctrine'

    #define admin service
    myproject_admin.project:
        class: MyProject\MyBundle\Admin\ProjectAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: Projects, label: Projects }
        arguments:
            - null
            - MyProject\MyBundle\Entity\Project
            - SonataAdminBundle:CRUD
        calls:
            - [setModelManager, ['@myproject.model_manager'] ]

Read more in following section of the documentation: http://sonata-project.org/bundles/admin/2-2/doc/reference/advanced.html