我正在使用 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”,倒數第二列)為空,請將“調整后指示”設為空。因為現在,即使沒有調整,我也可以放置一個指示PostAdjusting。
我知道 EasyAdmin 有一些解決方案,例如 createEntity、persistEntity 和 updateEntity,但我不知道如何在我的情況下使用它,您有什么想法嗎?
諾埃
- 編輯 -
我正在創建一個 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());
}
}
如果 Adjustings 是空的,我得到這個:

別的 :

我的新問題是:如何檢查我的陣列“元素”是否為空?
我怎樣才能做到呢?
uj5u.com熱心網友回復:
不確定是否理解您要實作的目標,但聽起來像是 Event 可以在 easyadmin 中執行的操作,例如 AfterEntityUpdatedEvent。這是一個每次更新特定物體時都會觸發的函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/490286.html
下一篇:WSL2Nginx PHPFPM在連接到上游時失敗(111:連接被拒絕),客戶端:172.23.0.1,上游:“fastcgi://172.23.0.3:9001”
