It is my first simple project on SymfonyCMF. In my project i have a lot of 'profile' documents(in term of phpcr). Each profile belongs to specific 'department'(parent document). (In phpcr each document must have parent document.) In admin panel (SonataAdmin) i can list all 'profiles' using configureListFields function. I can filter them by 'profile' properties using configureDatagridFilters function. But, cant figure out how to filter 'profile' by its parent 'department' document.
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('name','doctrine_phpcr_string')
->add('title', 'doctrine_phpcr_string')
->add('parent') <--- Need to filter by parent !
;
}
Tried to implement custom callback filter function from SonataAdmin documentation:
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('parent', 'doctrine_phpcr_callback', array(
'callback' => function($queryBuilder, $alias, $field, $data) {
if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
return;
}
$queryBuilder = $proxyQuery->getQueryBuilder();
$eb = $queryBuilder->expr();
$queryBuilder->andWhere($eb->eq($field, $data['value']));
return true;
},
'field_type' => 'checkbox'
))
;
}
But, first i got an error related to $proxyQuery, $queryBuilder->expr(). Second i have no idea how to properly query and filter by parent ((. Thanx in advance.
SOLVED thanx to @Bilel Noômene The final answer is
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('parent', 'doctrine_phpcr_callback', array('callback' =>
function ($proxyQuery, $alias, $field, $data) {
if (!$data || !is_array($data) || !array_key_exists('value', $data) || !$data['value']) {
return;
}
$queryBuilder = $proxyQuery->getQueryBuilder();
$queryBuilder->from($alias)
->joinInner()
->left()->document(Profile::class, $alias)->end()
->right()->document(Department::class, 'd')->end()
->condition()->child($alias, 'd')->end();
$queryBuilder->andWhere()->same($data['value']->getId(), 'd')->end();
return true;
}
), DocumentType::class, array('class'=>Department::class));
parent::configureDatagridFilters($datagridMapper);
}
The example of the documentation doesn't seem to be working correctly. For your case, try this code.
public function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('parent', 'doctrine_phpcr_callback', array('callback' =>
function ($proxyQuery, $alias, $field, $data) {
if (!$data || !is_array($data) || !array_key_exists('value', $data) || !$data['value']) {
return;
}
$queryBuilder = $proxyQuery->getQueryBuilder();
$queryBuilder->from($alias)
->joinInner()
->left()->document(Profile::class, $alias)->end()
->right()->document(Department::class, 'd')->end()
->condition()->child($alias, 'd')->end();
$queryBuilder->andWhere()->same($data['value']->getId(), 'd')->end();
return true;
}
), 'phpcr_document', array('class' => Department::class));
parent::configureDatagridFilters($datagridMapper);
}