拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 在Symfony中加载Fixtures时订阅者是否作业?

在Symfony中加载Fixtures时订阅者是否作业?

白鹭 - 2022-01-26 2007 0 0

我尝试使用命令在 Symfony 5 上运行下面的夹具php bin/console d:f:l我收到此错误:SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'contact_email' cannot be null

通过 CRUD 手动创建 Post 物体时,相同的代码逻辑可以正常作业。固定装置与订阅者(事件)不兼容还是我犯了错误?

谢谢你。

编辑:我也在使用 EasyAdmin Bundle 3。

应用\DataFixtures.php\AppFixtures.php

<?php

namespace App\DataFixtures;

use App\Entity\User;
use App\Entity\Author;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

class AppFixtures extends Fixture
{

    /** @var User[] */
    private $users = [];

    /** @var Author[] */
    private $authors = [];

    /** @var UserPasswordHasherInterface */
    private $hasher;

    public function __construct(UserPasswordHasherInterface $hasher)
    {
        $this->hasher = $hasher;
    }

    public function load(ObjectManager $manager): void
    {
        $this->createUsers();
        foreach($this->users as $user) $manager->persist($user);

        $this->createAuthors();
        foreach($this->authors as $author) $manager->persist($author);

        $manager->flush();
    }

    public function createUsers(): void
    {
        $admin = (new User)
            ->setUsername('admin')
            ->setEmail('admin@admin.com')
            ->setRoles(['ROLE_ADMIN'])
            ->setFirstname('Edouard')
            ->setLastname('Proust');
        $admin->setPassword($this->hasher->hashPassword($admin, 'admin'));

        $this->users[] = $admin;
    }

    public function createAuthors(): void
    {
        foreach($this->users as $user) {
            if(in_array('ROLE_ADMIN', $user->getRoles())) {
                $author = (new Author)
                    ->setUser($user)
                    ->setAvatar('#')
                    ->setBio('Bio')
                    // The line i want to get rid of:
                    // ->setContactEmail($user->getEmail())
                ;

                $this->authors[] = $author;
            }
        }
    }

}

App\EventListener\AuthorSubscriber.php

<?php

namespace App\EventListener;

use App\Entity\Author;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;

class AuthorSubscriber implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return [
            BeforeEntityPersistedEvent::class => 'setContactEmail',
        ];
    }

    public function setContactEmail(BeforeEntityPersistedEvent $event)
    {
        
        /** @var Author */
        $entity = $event->getEntityInstance();
        if($entity instanceof Author) {
            if(!$entity->getContactEmail()) {
                $user = $entity->getUser();
                $contactEmail = $user ? $user->getEmail() : '#';
                $entity->setContactEmail($contactEmail);
            }
            
        }
    }

}

uj5u.com热心网友回复:

EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent:class不是正确的 Symfony 事件名称。您可能应该使用Doctrine\ORM\Events::prePersist.

另请检查您的 DoctrineBundle 版本。如果你使用默认的 services.yaml 配置和低于 2.1 的 DoctrineBundle,你必须配置services.yaml

    App\EventListener\AuthorSubscriber:
        tags:
            - name: 'doctrine.event_subscriber'

你可以在这里阅读更多内容:https ://symfony.com/doc/current/doctrine/events.html#doctrine-lifecycle-subscribers

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *