如何使用会话 [Sonata Admin] 在过滤器中设置默认值
How have default value in filter with session [Sonata Admin]
我想为 filterParameters 的默认值设置动态会话值
此代码有效:
/**
* Default Datagrid values
*
* @var array
*/
protected $datagridValues = array(
'applications' => array('value' => 'Sport TV'),
'_sort_order' => 'ASC'
);
// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('title')
->add('applications', null, array('label' => 'Chaîne'), null, array('expanded' => true, 'multiple' => true));
}
但是当我添加会话时,他不希望我在
功能:
public function getApplicationsSession()
{
$session = new Session();
return $session->get('applications');
}
/**
* Default Datagrid values
*
* @var array
*/
protected $datagridValues = array(
'applications' => array('value' => $this->getApplicationsSession()),
'_sort_order' => 'ASC'
);
我有这个错误:
Parse Error: syntax error, unexpected '$this' (T_VARIABLE)
感谢您的帮助。
这部分代码是错误原因:
protected $datagridValues = array(
'applications' => array('value' => $this->getApplicationsSession()),
^---- syntax error !
'_sort_order' => 'ASC'
);
The pseudo-variable $this
is available when a method is called from within an object context. $this
is a reference to the calling object (usually the object to which the method belongs... http://php.net/manual/en/language.oop5.basic.php
要解决这个问题,您应该覆盖 getFilterParameters()
方法:
public function getFilterParameters()
{
$this->datagridValues['applications']['value'] = $this->getApplicationsSession();
return parent::getFilterParameters();
}
我想为 filterParameters 的默认值设置动态会话值
此代码有效:
/**
* Default Datagrid values
*
* @var array
*/
protected $datagridValues = array(
'applications' => array('value' => 'Sport TV'),
'_sort_order' => 'ASC'
);
// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('title')
->add('applications', null, array('label' => 'Chaîne'), null, array('expanded' => true, 'multiple' => true));
}
但是当我添加会话时,他不希望我在 功能:
public function getApplicationsSession()
{
$session = new Session();
return $session->get('applications');
}
/**
* Default Datagrid values
*
* @var array
*/
protected $datagridValues = array(
'applications' => array('value' => $this->getApplicationsSession()),
'_sort_order' => 'ASC'
);
我有这个错误:
Parse Error: syntax error, unexpected '$this' (T_VARIABLE)
感谢您的帮助。
这部分代码是错误原因:
protected $datagridValues = array(
'applications' => array('value' => $this->getApplicationsSession()),
^---- syntax error !
'_sort_order' => 'ASC'
);
The pseudo-variable
$this
is available when a method is called from within an object context.$this
is a reference to the calling object (usually the object to which the method belongs... http://php.net/manual/en/language.oop5.basic.php
要解决这个问题,您应该覆盖 getFilterParameters()
方法:
public function getFilterParameters()
{
$this->datagridValues['applications']['value'] = $this->getApplicationsSession();
return parent::getFilterParameters();
}