How to extends Sonata\DoctrineORMAdminBundle\Model

2019-05-10 04:45发布

I want some changes in ModelMangaer then I was extending ModelManager but It's not working. I don't know why ?

Any one tell me why it is not working?

File where I extend Sonata\DoctrineORMAdminBundle\Model\ModelManager->

<?php

use Sonata\DoctrineORMAdminBundle\Model\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']) {

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

3条回答
Melony?
2楼-- · 2019-05-10 04:49

You have to modify the service that is going to be injected, see:

Admin's documentation - Reference - Advanced (master) - 26.1. Service Configuration

# app/config/config.yml
admins:
sonata_admin:
    sonata.order.admin.order:   # id of the admin service this setting is for
        model_manager:          # dependency name, from the table above
            sonata.order.admin.order.manager  # customised service id

For an individual Model Manager on an Admin Class please see this answer: SonataDoctrineORM - Model extends

查看更多
走好不送
3楼-- · 2019-05-10 05:02

You forgot the namespace

namespace Acme\MyDoctrineORMAdminBundle\Model\ModelManager;

You need to use bundle inheritance.

// src/Acme/MyDoctrineORMAdminBundle/MyDoctrineORMAdminBundle.php

namespace Acme\MyDoctrineORMAdminBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class MyDoctrineORMAdminBundle extends Bundle
{
    public function getParent()
    {
        return 'DoctrineORMAdminBundle';
    }
}
查看更多
祖国的老花朵
4楼-- · 2019-05-10 05:09

You have a very big problem here:

class ModelManager extends ModelManager

You try to extend class from self. It's wrong! You need to declare your base class with Fully Qualified Name or use use statement. Also you forgot to put namespace declaration. Something like this will work:

namespace Acme\Bundle\DemoBundle\Model;

use Sonata\DoctrineORMAdminBundle\Model\ModelManager as BaseClass;

class ModelManager extends BaseClass
查看更多
登录 后发表回答