如果其他值在easyAdmin中的创造力或更新词为null,则将价值放在null上

发布于 2025-02-04 01:13:15 字数 4307 浏览 4 评论 0原文

我正在与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 :

enter image description here

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 :

enter image description here

Else :

enter image description here

My new question is : How can I check if my array "elements" is empty or not ?

How can I make it pls ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2025-02-11 01:13:15

不确定要了解您要实现的目标,但听起来像事件可以做的事情,例如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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文