ORO 平台 - 如何将相关实体的集合传递给数据网格列模板?
ORO platform - how to pass collection of related entities to datagrid column template?
我有一个实体与 ORO 平台中的相关实体。 Built-in User
实体可以有多个built-in Access Roles
。我想使用自定义树枝模板在用户数据网格中显示这些角色,以便我可以将它们包装在一些标签或样式中。
这是我的部分 datagrids.yaml,它覆盖了默认用户数据网格:
users-grid:
source:
query:
select:
- What to pass here?
join:
left:
- { join: u.roles, alias: r }
groupBy: u.id
columns:
roles:
inline_editing:
enable: false
label: 'Roles'
type: twig
frontend_type: html
template: 'MyBundle:User:Property/roles.html.twig'
我需要做什么才能访问 roles.html.twig
中的 Role 对象集合?
仅使用 YAML 配置是不可能的。相反,您必须创建一个数据网格侦听器,它将使用“result after”事件上的数据填充角色列。
这是一个例子:
- AcmeDemoBundle/EventListener/UserRolesGridListener.php
<?php
namespace AcmeDemoBundle\EventListener;
use Oro\Bundle\DataGridBundle\Datasource\ResultRecord;
use Oro\Bundle\DataGridBundle\Event\OrmResultAfter;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\UserBundle\Entity\User;
class UserRolesGridListener
{
/** @var DoctrineHelper */
protected $doctrineHelper;
/**
* @param DoctrineHelper $doctrineHelper
*/
public function __construct(DoctrineHelper $doctrineHelper)
{
$this->doctrineHelper = $doctrineHelper;
}
/**
* @param OrmResultAfter $event
*/
public function onResultAfter(OrmResultAfter $event)
{
$userIds = [];
/** @var ResultRecord[] $records */
$records = $event->getRecords();
foreach ($records as $record) {
$userIds[] = $record->getValue('id');
}
$repository = $this->doctrineHelper->getEntityRepository(User::class);
$userRoles = $repository->createQueryBuilder('user')
->select('partial user.{id}, partial roles.{id, label}')
->indexBy('user','user.id')
->leftJoin('user.roles','roles')
->where('user.id in (:userIds)')
->setParameter('userIds', $userIds)
->getQuery()
->getArrayResult();
foreach ($records as $record) {
$id = $record->getValue('id');
$record->addData(['roles' => $userRoles[$id]['roles']]);
}
}
}
- AcmeDemoBundle/Resources/config/services.yml
services:
AcmeDemoBundle\EventListener\UserRolesGridListener:
arguments:
- '@oro_entity.doctrine_helper'
tags:
- { name: kernel.event_listener, event: oro_datagrid.orm_datasource.result.after.users-grid, method: onResultAfter }
- AcmeDemoBundle/Resources/config/oro/datagrids.yml
datagrids:
users-grid:
columns:
roles:
inline_editing:
enable: false
label: 'Roles'
type: twig
frontend_type: html
template: 'AcmeDemoBundle:User:Column/roles.html.twig'
- AcmeDemoBundle/Resources/views/User/Column/roles.html.twig
{% for role in record.getValue('roles') %}
<div class="role-{{ role.id }}">{{ role.label }}</div>
{% endfor %}
我有一个实体与 ORO 平台中的相关实体。 Built-in User
实体可以有多个built-in Access Roles
。我想使用自定义树枝模板在用户数据网格中显示这些角色,以便我可以将它们包装在一些标签或样式中。
这是我的部分 datagrids.yaml,它覆盖了默认用户数据网格:
users-grid:
source:
query:
select:
- What to pass here?
join:
left:
- { join: u.roles, alias: r }
groupBy: u.id
columns:
roles:
inline_editing:
enable: false
label: 'Roles'
type: twig
frontend_type: html
template: 'MyBundle:User:Property/roles.html.twig'
我需要做什么才能访问 roles.html.twig
中的 Role 对象集合?
仅使用 YAML 配置是不可能的。相反,您必须创建一个数据网格侦听器,它将使用“result after”事件上的数据填充角色列。 这是一个例子:
- AcmeDemoBundle/EventListener/UserRolesGridListener.php
<?php
namespace AcmeDemoBundle\EventListener;
use Oro\Bundle\DataGridBundle\Datasource\ResultRecord;
use Oro\Bundle\DataGridBundle\Event\OrmResultAfter;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\UserBundle\Entity\User;
class UserRolesGridListener
{
/** @var DoctrineHelper */
protected $doctrineHelper;
/**
* @param DoctrineHelper $doctrineHelper
*/
public function __construct(DoctrineHelper $doctrineHelper)
{
$this->doctrineHelper = $doctrineHelper;
}
/**
* @param OrmResultAfter $event
*/
public function onResultAfter(OrmResultAfter $event)
{
$userIds = [];
/** @var ResultRecord[] $records */
$records = $event->getRecords();
foreach ($records as $record) {
$userIds[] = $record->getValue('id');
}
$repository = $this->doctrineHelper->getEntityRepository(User::class);
$userRoles = $repository->createQueryBuilder('user')
->select('partial user.{id}, partial roles.{id, label}')
->indexBy('user','user.id')
->leftJoin('user.roles','roles')
->where('user.id in (:userIds)')
->setParameter('userIds', $userIds)
->getQuery()
->getArrayResult();
foreach ($records as $record) {
$id = $record->getValue('id');
$record->addData(['roles' => $userRoles[$id]['roles']]);
}
}
}
- AcmeDemoBundle/Resources/config/services.yml
services:
AcmeDemoBundle\EventListener\UserRolesGridListener:
arguments:
- '@oro_entity.doctrine_helper'
tags:
- { name: kernel.event_listener, event: oro_datagrid.orm_datasource.result.after.users-grid, method: onResultAfter }
- AcmeDemoBundle/Resources/config/oro/datagrids.yml
datagrids:
users-grid:
columns:
roles:
inline_editing:
enable: false
label: 'Roles'
type: twig
frontend_type: html
template: 'AcmeDemoBundle:User:Column/roles.html.twig'
- AcmeDemoBundle/Resources/views/User/Column/roles.html.twig
{% for role in record.getValue('roles') %}
<div class="role-{{ role.id }}">{{ role.label }}</div>
{% endfor %}