Sonata Block Bundle Edit 表单不保存 EntityType
Sonata Block Bundle Edit form dont save EntityType
这是我的街区,
当我保存时 - 标题被存储购买文章为空,错误在哪里?
class ArticleBlock extends AbstractAdminBlockService
{
/**
* {@inheritdoc}
*/
public function configureSettings(OptionsResolver $resolver)
{
$resolver->setDefaults([
'article' => null,
'title' => null,
'template' => '@MeaArticleBundle/Sonata/Templates/article_block.html.twig',
]);
}
/**
* {@inheritdoc}
*/
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
$formMapper->add('settings', ImmutableArrayType::class, [
'keys' => [
['article', EntityType::class , [
'class' => Article::class,
'required' => true,
'property' => 'title',
'label' => 'Article',
]],
['title', TextType::class, [
'label' => 'form.label_title',
'required' => false,
]],
],
'translation_domain' => 'SonataBlockBundle',
]);
}
/**
* {@inheritdoc}
*/
public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
{
//var_dump($block);
$errorElement
->with('article[article]')
->assertNotNull([])
->assertNotBlank()
->end()
->with('title[title]')
->assertNotNull([])
->assertNotBlank()
// ->assertLength(['max' => 50])
->end();
}
/**
* {@inheritdoc}
*/
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
// merge settings
$settings = $blockContext->getSettings();
var_dump([$blockContext,$settings]);
保存后得到
"use_cache" => true
"extra_cache_keys" => array:2 [▶]
"attr" => []
"template" => "@MeaArticleBundle/Sonata/Templates/article_block.html.twig"
"ttl" => 86400
"manager" => "snapshot"
"page_id" => 1
"article" => []
"title" => "test2"
]
我创建了一个将所选实体的 ID 映射到数组的方法:
private function mapTestimonialsToIds(array $testimonials): array
{
$ids = [];
/** @var Testimonial $testimonial */
foreach ($testimonials as $testimonial) {
$ids[] = $testimonial->getId();
}
return $ids;
}
我在 prePersists 和 preUpdate 方法中都调用了这个方法,并将其设置为我的块推荐设置作为这种情况下的值。
然后,在执行方法中,我通过使用保存的 ID 和 return 将这些找到的实体获取到我的模板中。
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
return $this->renderResponse($blockContext->getTemplate(), [
'testimonials' => $this->testimonialRepository->findByIds($blockContext->getSetting('testimonials')),
'block' => $blockContext->getBlock(),
], $response);
}
这是我的街区,
当我保存时 - 标题被存储购买文章为空,错误在哪里?
class ArticleBlock extends AbstractAdminBlockService
{
/**
* {@inheritdoc}
*/
public function configureSettings(OptionsResolver $resolver)
{
$resolver->setDefaults([
'article' => null,
'title' => null,
'template' => '@MeaArticleBundle/Sonata/Templates/article_block.html.twig',
]);
}
/**
* {@inheritdoc}
*/
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
$formMapper->add('settings', ImmutableArrayType::class, [
'keys' => [
['article', EntityType::class , [
'class' => Article::class,
'required' => true,
'property' => 'title',
'label' => 'Article',
]],
['title', TextType::class, [
'label' => 'form.label_title',
'required' => false,
]],
],
'translation_domain' => 'SonataBlockBundle',
]);
}
/**
* {@inheritdoc}
*/
public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
{
//var_dump($block);
$errorElement
->with('article[article]')
->assertNotNull([])
->assertNotBlank()
->end()
->with('title[title]')
->assertNotNull([])
->assertNotBlank()
// ->assertLength(['max' => 50])
->end();
}
/**
* {@inheritdoc}
*/
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
// merge settings
$settings = $blockContext->getSettings();
var_dump([$blockContext,$settings]);
保存后得到
"use_cache" => true
"extra_cache_keys" => array:2 [▶]
"attr" => []
"template" => "@MeaArticleBundle/Sonata/Templates/article_block.html.twig"
"ttl" => 86400
"manager" => "snapshot"
"page_id" => 1
"article" => []
"title" => "test2"
]
我创建了一个将所选实体的 ID 映射到数组的方法:
private function mapTestimonialsToIds(array $testimonials): array
{
$ids = [];
/** @var Testimonial $testimonial */
foreach ($testimonials as $testimonial) {
$ids[] = $testimonial->getId();
}
return $ids;
}
我在 prePersists 和 preUpdate 方法中都调用了这个方法,并将其设置为我的块推荐设置作为这种情况下的值。
然后,在执行方法中,我通过使用保存的 ID 和 return 将这些找到的实体获取到我的模板中。
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
return $this->renderResponse($blockContext->getTemplate(), [
'testimonials' => $this->testimonialRepository->findByIds($blockContext->getSetting('testimonials')),
'block' => $blockContext->getBlock(),
], $response);
}