将SonataAdmin实体的数据网格列表嵌入到另一个实体EditForm的最佳方法是什么?
我在文档中找不到该过程。
感谢您的帮助
转到所需的列表。从浏览器中复制网址:
现在使用 jQuery:
$.get(url_you_just_copied, function(result){
console.log(result);
};
看看你从listAction调用中得到什么,并相应地调整请求参数(过滤器等(以获得你想要的列表。
接下来要有一个干净的解决方案,您需要使用 twig 助手生成您的 url:https://stackoverflow.com/a/15857401/5758328
以下是我如何使用sonataBlockBundle来显示电子邮件列表:
块服务类:
namespace LibrinfoEmailBundleBlock;
use DoctrineORMEntityManager;
use LibrinfoEmailBundleEntityEmail;
use SonataBlockBundleBlockBlockContextInterface;
use SonataBlockBundleBlockServiceTextBlockService;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentHttpFoundationResponse;
class EmailsListBlock extends TextBlockService
{
/**
* @var EntityManager
*/
protected $manager;
/**
* @param EntityManager $manager
*/
public function setManager(EntityManager $manager) {
$this->manager = $manager;
}
/**
* {@inheritdoc}
*/
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
$settings = $blockContext->getSettings();
$targetEntity = $settings['target_entity'];
$maxResults = $settings['max_results'];
$emails = $this->getEmails($targetEntity, $maxResults);
return $this->renderResponse($blockContext->getTemplate(), array(
'block' => $blockContext->getBlock(),
'settings' => $settings,
'emails' => $emails,
), $response);
}
/**
* {@inheritdoc}
*/
public function configureSettings(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'content' => 'Insert your custom content here',
'template' => 'LibrinfoEmailBundle:Block:block_emails_list.html.twig',
'target_entity' => null,
'max_results' => 20,
));
}
/**
* @param object $targetEntity
* @param int $maxResults
* @return array
* @throws Exception
*/
private function getEmails($targetEntity, $maxResults)
{
if (!$targetEntity || !is_object($targetEntity))
return [];
$rc = new ReflectionClass($targetEntity);
if (!$rc->hasProperty('emailMessages'))
return [];
$repo = $this->manager->getRepository($rc->getName());
if (method_exists($repo, 'getEmailMessagesQueryBuilder')) {
$qb = $repo->getEmailMessagesQueryBuilder($targetEntity->getId());
}
else {
$repo = $this->manager->getRepository(Email::class);
$targets = strtolower($rc->getShortName()) . 's'; // ex. contacts
$qb = $repo->createQueryBuilder('e')
->leftJoin('e.'.$targets, 't')
->where('t.id = :targetid')
->setParameter('targetid', $targetEntity->getId())
;
}
$qb->orderBy('e.updatedAt', 'desc')
->setMaxResults($maxResults);
return $qb->getQuery()->getResult();
}
}
服务定义:
librinfo.email.block.emails_list:
class: LibrinfoEmailBundleBlockEmailsListBlock
arguments:
- librinfo.email.block.emails_list
- '@templating'
calls:
- [setManager, [@doctrine.orm.entity_manager]]
tags: [{ name: sonata.block }]
和模板(缩写(
{% extends sonata_block.templates.block_base %}
{% block block %}
<table class="table table-bordered table-striped sonata-ba-list emails-history">
<thead>
<tr>
<th>Expéditeur</th>
<th>Destinataires</th>
<th>Objet</th>
<th>Envoyé</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for email in emails %}
<tr data-email-id="{{ email.id }}">
then use it like this in your edit template:
{{ sonata_block_render({'type': 'librinfo.email.block.emails_list'}, {'target_entity': object}) }}