symfony 对null调用成员函数get...()

lmyy7pcs  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(127)

大家好,我需要帮助.
我试图建立一个论坛,为我的网站在symfony。我有一个实体主题谁重组实体鼻涕虫谁重组实体后。
当我导航到slug路径查看帖子时,我遇到了错误“在null上调用成员函数getSlug()”。但是这个函数是在另一个路径中调用的。
这里是我的不同实体。
布景主题

<?php

namespace App\Entity;

use App\Repository\ThemeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ThemeRepository::class)]
class Theme
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\Column(type: 'string', length: 255)]
    private $name;

    #[ORM\Column(type: 'string', length: 255, nullable: true)]
    private $imageName;

    #[ORM\OneToMany(mappedBy: 'theme', targetEntity: Slug::class, orphanRemoval: true)]
    private $slug;

    public function __construct()
    {
        $this->slug = new ArrayCollection();
    }

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

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getImageName(): ?string
    {
        return $this->imageName;
    }

    public function setImageName(?string $imageName): self
    {
        $this->imageName = $imageName;

        return $this;
    }

    /**
     * @return Collection<int, Slug>
     */
    public function getSlug(): Collection
    {
        return $this->slug;
    }

    public function addSlug(Slug $slug): self
    {
        if (!$this->slug->contains($slug)) {
            $this->slug[] = $slug;
            $slug->setTheme($this);
        }

        return $this;
    }

    public function removeSlug(Slug $slug): self
    {
        if ($this->slug->removeElement($slug)) {
            // set the owning side to null (unless already changed)
            if ($slug->getTheme() === $this) {
                $slug->setTheme(null);
            }
        }

        return $this;
    }
}

段塞

<?php

namespace App\Entity;

use App\Repository\SlugRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: SlugRepository::class)]
class Slug
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\Column(type: 'string', length: 255)]
    private $slugName;

    #[ORM\OneToMany(mappedBy: 'slug', targetEntity: Post::class, orphanRemoval: true)]
    private $posts;

    #[ORM\ManyToOne(targetEntity: Theme::class, inversedBy: 'slug')]
    #[ORM\JoinColumn(nullable: false)]
    private $theme;

    public function __construct()
    {
        $this->posts = new ArrayCollection();
    }

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

    public function getSlugName(): ?string
    {
        return $this->slugName;
    }

    public function setSlugName(string $slugName): self
    {
        $this->slugName = $slugName;

        return $this;
    }

    /**
     * @return Collection<int, Post>
     */
    public function getPosts(): Collection
    {
        return $this->posts;
    }

    public function addPost(Post $post): self
    {
        if (!$this->posts->contains($post)) {
            $this->posts[] = $post;
            $post->setSlug($this);
        }

        return $this;
    }

    public function removePost(Post $post): self
    {
        if ($this->posts->removeElement($post)) {
            // set the owning side to null (unless already changed)
            if ($post->getSlug() === $this) {
                $post->setSlug(null);
            }
        }

        return $this;
    }

    public function getTheme(): ?Theme
    {
        return $this->theme;
    }

    public function setTheme(?Theme $theme): self
    {
        $this->theme = $theme;

        return $this;
    }
}

岗位

<?php

namespace App\Entity;

use App\Repository\PostRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: PostRepository::class)]
class Post
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\Column(type: 'text')]
    private $content;

    #[ORM\Column(type: 'datetime_immutable')]
    private $createdAt;

    #[ORM\Column(type: 'datetime_immutable', nullable: true)]
    private $updatedAt;

    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'posts')]
    #[ORM\JoinColumn(nullable: false)]
    private $user;

    #[ORM\ManyToOne(targetEntity: Slug::class, inversedBy: 'posts')]
    #[ORM\JoinColumn(nullable: false)]
    private $slug;

    #[ORM\Column(type: 'string', length: 255, nullable: true)]
    private $imageName;

    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 getContent(): ?string
    {
        return $this->content;
    }

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

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function setCreatedAt(\DateTimeImmutable $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    public function getUpdatedAt(): ?\DateTimeImmutable
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt(?\DateTimeImmutable $updatedAt): self
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;

        return $this;
    }

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

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

        return $this;
    }

    public function getImageName(): ?string
    {
        return $this->imageName;
    }

    public function setImageName(?string $imageName): self
    {
        $this->imageName = $imageName;

        return $this;
    }
}

这是我的控制器

<?php

namespace App\Controller;

use App\Repository\SlugRepository;
use App\Repository\ThemeRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ForumController extends AbstractController
{
    #[Route('/forum', name: 'forum')]
    public function index(SlugRepository $slugRepository, ThemeRepository $themeRepository ): Response
    {
        $themes = $themeRepository->findAll();
        return $this->render('forum/index.html.twig', [
            'themes' => $themes,
        ]);
    }

    #[Route('/forum/{name}/{id}', name: 'theme')]
    public function theme(string $name, $id, ThemeRepository $themeRepository ): Response
    {
        $themesId = $themeRepository->find($id);
        $themes = $themeRepository->find($name);
        $slugs = $themesId->getSlug();
        $allThemes = $themeRepository->findAll();
        return $this->render('forum/theme.html.twig', [
            'themes' => $themesId,
            'slugs' => $slugs,
            'allThemes' => $allThemes,
        ]);
    }

    #[Route('/forum/{name}/{slugName}', name: 'slug')]
    public function slug(string $name, string $slugName, $id,SlugRepository $slugRepository, ThemeRepository $themeRepository ): Response
    {
        $slugName = $slugRepository->find($slugName);
        $slugsId = $slugRepository->find($id);
        $themesId = $slugsId->getTheme()->getName($name);
        $allThemes = $themeRepository->findAll();
        return $this->render('forum/slug.html.twig', [
            'themes' => $themesId,
            'slugs' => $slugsId,
            'allThemes' => $allThemes,
        ]);
    }
}

我正在编写symfony 5.0.8
谢谢你的帮助。

fzsnzjdm

fzsnzjdm1#

ForumController::theme方法中,$themeRepository->find()方法不返回Theme对象,但null$themes未使用。

$themesId = $themeRepository->find($id); // <- returns null

解决方案:按idname显式查询。

$theme = $themeRepository->findOneBy([
 'id' => $id,
 'name' => $name
]);

if (!$theme) {
  // log id and name, return a 404
}

$slug = $theme->getSlug();

相关问题