【问题标题】:Call to a member function get...() on null在 null 上调用成员函数 get...()
【发布时间】:2022-12-21 08:23:44
【问题描述】:

大家好,我需要帮助。

我正在尝试在 symfony 中为我的网站建立一个论坛。 我有一个实体 Theme,它重新组合实体 Slug,它重新组合实体 Post。

当我导航到 slug 路由以查看帖子时,出现错误“Call to a member function getSlug() on null”。 但是这个函数是在另一个路由中调用的。

这是我的不同实体。

主题

<?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

谢谢你的帮助。

【问题讨论】:

    标签: php symfony forum


    【解决方案1】:

    在您的方法 ForumController::theme 中,方法 $themeRepository-&gt;find() 不返回 Theme 对象,但 null 和 $themes 未使用。

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

    解决方案:通过id和name显式查询。

    $theme = $themeRepository->findOneBy([
     'id' => $id,
     'name' => $name
    ]);
    
    if (!$theme) {
      // log id and name, return a 404
    }
    
    $slug = $theme->getSlug();
    

    【讨论】:

    • 谢谢你的帮助。但我想我说错了。当我使用主题路线时,没有问题。当我使用 slug 路由时,错误来自主题路由。我不明白为什么使用主题路由我的方法 $slugs = $themesId->getSlug() 工作并显示 slug 与主题相关,并且当我使用 slug 路由时此方法返回 null。
    猜你喜欢
    • 1970-01-01
    • 2021-10-27
    • 2021-01-23
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 2017-08-09
    • 2020-03-10
    相关资源
    最近更新 更多