感谢 prodigitalson 的评论,我通过传递参数解决了您建议的问题。
现在我的 CustomerAdmin.php 扩展了覆盖 Admin 的 AbstractAdmin 类。这个 AbstractAdmin 包含公共代码,所有其他 Admin 类都扩展了这个抽象类。
<?php
namespace xxxx\AdminBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Doctrine\ORM\EntityManager;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
abstract class AbstractAdmin extends Admin
{
/** @var int */
protected $maxPerPage = 10;
//other attributes
public function __construct($code, $class, $baseControllerName)
{
parent::__construct($code, $class, $baseControllerName);
$this->fields = $this->sortFields($fields);
// custome arguments
if (!$this->hasRequest()) {
$this->datagridValues = array(
***'_per_page' => $this->maxPerPage*** //passing ***_per_page*** argument
);
}
}
<?php
namespace xxxx\AdminBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Route\RouteCollection;
class CustomerAdmin extends AbstractAdmin
{
//code here
}