Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当我创建一篇文章时,preUpdate和postapdate事件不是';没有触发。有人知道如何解决这个问题吗?_Php_Doctrine_Symfony4 - Fatal编程技术网

Php 当我创建一篇文章时,preUpdate和postapdate事件不是';没有触发。有人知道如何解决这个问题吗?

Php 当我创建一篇文章时,preUpdate和postapdate事件不是';没有触发。有人知道如何解决这个问题吗?,php,doctrine,symfony4,Php,Doctrine,Symfony4,我按照上面的说明做了,我不知道我做错了什么。但这些事件并没有被触发 事件侦听器获取slug,如果slug相同,则将文章中的Id放入slug。 namespace App\EventListener; use App\Entity\Article; use App\Repository\ArticleRepository; use App\Service\SlugifyService; use Doctrine\Common\EventSubscriber; use Doctrine\Comm

我按照上面的说明做了,我不知道我做错了什么。但这些事件并没有被触发

事件侦听器获取slug,如果slug相同,则将文章中的Id放入slug。
namespace App\EventListener;


use App\Entity\Article;
use App\Repository\ArticleRepository;
use App\Service\SlugifyService;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Events;


class SlugIndexer implements EventSubscriber
{
    private $repository;
    private $slugify;

public function __construct(ArticleRepository $repository, SlugifyService $slugifyService)
{
    $this->repository = $repository;
    $this->slugify = $slugifyService;
}
public function getSubscribedEvents()
{
    return [
        Events::postPersist,
        Events::preUpdate,
    ];
}

public function preUpdate(Article $article, PreUpdateEventArgs $event)
{
    $articleSlug = $this->slugify->slugify($article->getTitle());
    if ($this->repository->findOneBy(['slug' => $articleSlug])) {
        $articleSlug .= '-' . $article->getId();
    }

    $article->setSlug($articleSlug);
}

public function postPersist(LifecycleEventArgs $args)
{
    $article = $args->getObject();

    if (!($article instanceof Article)) {
        return;
    }

    $articleSlug = $this->slugify->slugify($article->getTitle());
    if ($this->repository->findOneBy(['slug' => $articleSlug])) {
        $articleSlug .= '-' . $article->getId();
    }
    $article->setSlug($articleSlug);
    $args->getObjectManager()->persist($article);
    $args->getObjectManager()->flush();

}
}

这是我的文章实体中的代码

<?php

namespace App\Entity;

use App\Entity\User\User;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
 * @ORM\Table(name="articles")
 */
class Article implements EntityInterface
{

    /**
     * @var int
     *
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     *
     */
    private $title;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $lead;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     *
     */
    private $slug;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $content;

    /**
     * @ORM\Column(type="datetime")
     */
    private $publishedAt;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="articles")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
     *
     */
    private $category;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User\User", inversedBy="articles")
     * @ORM\JoinColumn(nullable=false)
     */
    private $author;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getLead(): ?string
    {
        return $this->lead;
    }

    public function setLead(string $lead): self
    {
        $this->lead = $lead;

        return $this;
    }

    public function getSlug(): ?string
    {
        return $this->slug;
    }

    public function setSlug(string $slug): self
    {
        $this->slug = $slug;

        return $this;
    }

    public function getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

    public function getPublishedAt(): ?\DateTimeInterface
    {
        return $this->publishedAt;
    }

    public function setPublishedAt(\DateTimeInterface $publishedAt): self
    {
        $this->publishedAt = $publishedAt;

        return $this;
    }

    public function getCategory(): ?Category
    {
        return $this->category;
    }

    public function setCategory(?Category $category): self
    {
        $this->category = $category;

        return $this;
    }

    public function getAuthor(): ?User
    {
        return $this->author;
    }

    public function setAuthor(?User $author): self
    {
        $this->author = $author;

        return $this;
    }
}

我编辑了EvenetListener,现在所有的作品

<?php


namespace App\EventListener;


use App\Entity\Article;
use App\Repository\ArticleRepository;
use App\Service\SlugifyService;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;


class SlugIndexer implements EventSubscriber
{
    private $repository;
    private $slugify;

    public function __construct(ArticleRepository $repository, SlugifyService $slugifyService)
    {
        $this->repository = $repository;
        $this->slugify = $slugifyService;
    }
    public function getSubscribedEvents()
    {
        return [
            Events::preUpdate,
            Events::postPersist,
        ];
    }

    public function preUpdate(LifecycleEventArgs $args)
    {
        $article = $args->getObject();
        $articleSlug = $this->slugify->slugify($article->getTitle());
        if ($this->repository->findOneBy(['slug' => $articleSlug])) {
            $articleSlug .= '-' . $article->getId();
        }
        $article->setSlug($articleSlug);
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        $article = $args->getObject();

        if (!($article instanceof Article)) {
            return;
        }

        $articleSlug = $this->slugify->slugify($article->getTitle());
        if ($this->repository->findOneBy(['slug' => $articleSlug])) {
            $articleSlug .= '-' . $article->getId();
        }
        $article->setSlug($articleSlug);
        $args->getObjectManager()->persist($article);
        $args->getObjectManager()->flush();

    }

services.yaml文件看起来像这个App\EventListener\SlugIndexer:tags:-{name:'doctrine.event\u listener',event:'preUpdate'}-{name:'doctrine.event\u listener',event:'postPersist'}
<?php


namespace App\EventListener;


use App\Entity\Article;
use App\Repository\ArticleRepository;
use App\Service\SlugifyService;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;


class SlugIndexer implements EventSubscriber
{
    private $repository;
    private $slugify;

    public function __construct(ArticleRepository $repository, SlugifyService $slugifyService)
    {
        $this->repository = $repository;
        $this->slugify = $slugifyService;
    }
    public function getSubscribedEvents()
    {
        return [
            Events::preUpdate,
            Events::postPersist,
        ];
    }

    public function preUpdate(LifecycleEventArgs $args)
    {
        $article = $args->getObject();
        $articleSlug = $this->slugify->slugify($article->getTitle());
        if ($this->repository->findOneBy(['slug' => $articleSlug])) {
            $articleSlug .= '-' . $article->getId();
        }
        $article->setSlug($articleSlug);
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        $article = $args->getObject();

        if (!($article instanceof Article)) {
            return;
        }

        $articleSlug = $this->slugify->slugify($article->getTitle());
        if ($this->repository->findOneBy(['slug' => $articleSlug])) {
            $articleSlug .= '-' . $article->getId();
        }
        $article->setSlug($articleSlug);
        $args->getObjectManager()->persist($article);
        $args->getObjectManager()->flush();

    }
   /**
 * @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
 * @ORM\Table(name="articles")
 * @ORM\HasLifecycleCallbacks
 */