我正在修复在 PHP 8.0.18 上运行的现有 Symfony 5.4 网站。后台由 EasyAdmin 3.4 处理。
我不知道出了什么问题。正如标题所说,当我去编辑一个“事件”实体时,除非我重新上传不同的事件图片,否则保存按钮根本不起作用。再多的编辑其他字段都不起作用,即使我没有对实体进行任何修改,我也可以在其他实体上使用保存按钮。我查看了我的配置和实体设置,但到目前为止,我不明白。
编辑:除非我重新上传了一些东西,否则带有 ImageField 的其他实体也拒绝更新。我通过setRequired(false)在事件 crud conf 中使用找到了一个临时修复,但在这种情况下肯定需要图像,所以如果我没记错的话,我只是在为另一种失败做好准备。这真的是唯一的方法吗?
setRequired(false)
活动实体:
<?php namespace App\Entity; use App\Repository\EventRepository; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass=EventRepository::class) */ class Event { // ... /** * @ORM\Column(type="string", length=255) */ private $src; // ... public function getSrc(): ?string { return $this->src; } public function setSrc(string $src): self { $this->src = $src; return $this; } // ... }
事件 crud 控制器:
<?php namespace App\Controller\Admin; use App\Entity\Event; use App\Entity\TranslationString; use App\Entity\TranslationText; use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField; use EasyCorp\Bundle\EasyAdminBundle\Field\DateField; use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField; use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField; use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField; class EventCrudController extends AbstractCrudController { public static function getEntityFqcn(): string { return Event::class; } public function configureCrud(Crud $crud): Crud { return $crud ->setSearchFields([ 'date', 'end', 'title.fr', 'title.en', 'body.fr', 'body.en', 'alt.fr', 'alt.en', ]) ->setDefaultSort(['archived' => 'ASC','date' => 'DESC',]); } public function configureFields(string $pageName): iterable { return [ DateField::new('date'), DateField::new('end'), TextField::new('titleFr'), TextField::new('titleEn')->hideOnIndex(), BooleanField::new('isShow'), BooleanField::new('archived'), TextareaField::new('bodyFr'), TextareaField::new('bodyEn')->hideOnIndex(), ImageField::new('src') ->setBasePath('img/events') ->setUploadDir('www/img/events'), TextareaField::new('altFr')->hideOnIndex(), TextareaField::new('altEn')->hideOnIndex(), ]; } public function createEntity(string $Fqcn): Event { return (new Event) ->setAlt(new TranslationText) ->setTitle(new TranslationString) ->setBody(new TranslationText); } }
我有同样的问题,我认为下面的代码会有所帮助
public function configureFields(string $pageName): iterable { $imageField = ImageField::new('image', 'Image')->setUploadDir('public/uploads/images/')->setBasePath('uploads/images/'); if ($pageName != 'new') { $imageField->setRequired(false); } return [ TextField::new('title'), $imageField, TextEditorField::new('description')->setNumOfRows(20), UrlField::new('ticketOfficeLink'), AssociationField::new('eventStates') ]; }