如果其他值在easyAdmin中的创造力或更新词为null,则将价值放在null上
我正在与Symfony和EasyAdmin合作。
这是我的渲染:
,这是指示代码:
<?php
namespace App\Controller\Admin;
use App\Entity\Indication;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Field\ColorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class IndicationCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Indication::class;
}
public function configureFields(string $pageName): iterable
{
return [
AssociationField::new('deficiencies', 'Déficience')
->formatValue(function ($value, $entity) {
return implode(", ",$entity->getDeficiencies()->toArray());
})
->setTemplatePath('admin/associationTemplate.html.twig')
->setRequired(true),
AssociationField::new('underActivities', 'Sous-activité')
->formatValue(function ($value, $entity) {
return implode(", ",$entity->getUnderActivities()->toArray());
})
->setTemplatePath('admin/associationTemplate.html.twig')
->setRequired(true),
ChoiceField::new('indicationPreAdjusting', 'Indice pré-aménagement')
->setChoices([
'Vert' => 'Green',
'Orange' => 'Orange',
'Rouge' => 'Red',
])
->onlyOnForms(),
ColorField::new('indicationPreAdjusting', 'Indice pré-aménagement')->hideOnForm(),
AssociationField::new('adjustings', 'Aménagement')
->formatValue(function ($value, $entity) {
return implode(", ",$entity->getAdjustings()->toArray());
})
->setTemplatePath('admin/associationTemplate.html.twig'),
ChoiceField::new('indicationPostAdjusting', 'Indice post-aménagement')
->setChoices([
'Vert' => 'Green',
'Orange' => 'Orange',
'Rouge' => 'Red',
])
->onlyOnForms(),
ColorField::new('indicationPostAdjusting', 'Indice post-aménagement')->hideOnForm(),
];
}
public function configureCrud(Crud $crud): Crud
{
return $crud
->setSearchFields(['deficiencies.name', 'underActivities.name'])
->setEntityLabelInSingular('Indication')
->setEntityLabelInPlural('Indications');
}
}
我想要“调整”(“Aménagement”,倒数第二列)是无效的调整“零”。因为现在,即使没有调整(S),我也可以提示指示。
我知道有一些更轻松的解决方案,例如创造性,持久性和更新性,但是我不知道如何在我的情况下使用它,您有任何想法吗?
noé
- 编辑 -
我正在创建一个easyadminsubscriber,例如:
<?php
namespace App\EventSubscriber;
use App\Entity\Indication;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['setIndicationPostAdjustingOnNullIfNoAdjustings'],
];
}
public function setIndicationPostAdjustingOnNullIfNoAdjustings(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Indication)) {
return;
}
dump($entity->getAdjustings());
}
}
如果调整为空,我得到了:
else:
问题是:如何检查我的数组“元素”是否为空?
我该怎么做?
I'm working with Symfony and EasyAdmin.
Here is my render :
And here is the Indication code :
<?php
namespace App\Controller\Admin;
use App\Entity\Indication;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Field\ColorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class IndicationCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Indication::class;
}
public function configureFields(string $pageName): iterable
{
return [
AssociationField::new('deficiencies', 'Déficience')
->formatValue(function ($value, $entity) {
return implode(", ",$entity->getDeficiencies()->toArray());
})
->setTemplatePath('admin/associationTemplate.html.twig')
->setRequired(true),
AssociationField::new('underActivities', 'Sous-activité')
->formatValue(function ($value, $entity) {
return implode(", ",$entity->getUnderActivities()->toArray());
})
->setTemplatePath('admin/associationTemplate.html.twig')
->setRequired(true),
ChoiceField::new('indicationPreAdjusting', 'Indice pré-aménagement')
->setChoices([
'Vert' => 'Green',
'Orange' => 'Orange',
'Rouge' => 'Red',
])
->onlyOnForms(),
ColorField::new('indicationPreAdjusting', 'Indice pré-aménagement')->hideOnForm(),
AssociationField::new('adjustings', 'Aménagement')
->formatValue(function ($value, $entity) {
return implode(", ",$entity->getAdjustings()->toArray());
})
->setTemplatePath('admin/associationTemplate.html.twig'),
ChoiceField::new('indicationPostAdjusting', 'Indice post-aménagement')
->setChoices([
'Vert' => 'Green',
'Orange' => 'Orange',
'Rouge' => 'Red',
])
->onlyOnForms(),
ColorField::new('indicationPostAdjusting', 'Indice post-aménagement')->hideOnForm(),
];
}
public function configureCrud(Crud $crud): Crud
{
return $crud
->setSearchFields(['deficiencies.name', 'underActivities.name'])
->setEntityLabelInSingular('Indication')
->setEntityLabelInPlural('Indications');
}
}
I want if "adjustings" ("Aménagement", the penultimate column) is null, put the "indicationPostAdjusting" on null. Because now, I can put an indicationPostAdjusting even if there is no adjusting(s).
I know there is some solutions with EasyAdmin like createEntity, persistEntity and updateEntity, but I don't know how to use it in my case, do you have any ideas ?
Noé
-- EDIT --
I'm creating an EasyAdminSubscriber like :
<?php
namespace App\EventSubscriber;
use App\Entity\Indication;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['setIndicationPostAdjustingOnNullIfNoAdjustings'],
];
}
public function setIndicationPostAdjustingOnNullIfNoAdjustings(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Indication)) {
return;
}
dump($entity->getAdjustings());
}
}
If Adjustings is empty, I got this :
Else :
My new question is : How can I check if my array "elements" is empty or not ?
How can I make it pls ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定要了解您要实现的目标,但听起来像事件可以做的事情,例如EasyAdmin,例如AfterEntityUpdatedEvent。这是一个函数,每次更新特定实体时都会触发。
Not sure to understand what you are trying to achieve but sound like something Event can do, like AfterEntityUpdatedEvent, in easyadmin. It's a function which will trigger each time you update a specific entity.