У меня есть таблица Article, в которой разрешено иметь много комментариев.
И когда я использую findBy, она не находит ничего для этой статьи.
Вот мое шоу.
$comments = $commentRepository->findBy(['article' => $article]);
dump($comments);die;
, который просто выводит.
ArticleController.php on line 52:
[]
Когда я все же нахожу все, я получаю 20 результатов, которые выглядят следующим образом
$allComments = $commentRepository->findAll();
dump($allComments);die;
0 => Comment^ {#881 ▼
-id: 1
-authorName: "Author name here"
-content: "Here is the comment content"
-article: Article^ {#809 ▼
+__isInitialized__: false
-id: 117
-title: null
-slug: null
-content: null
-publishedAt: null
-author: null
-heartCount: 0
-imageFilename: null
-createdAt: null
-updatedAt: null
-comments: null
…2
}
Вот полный метод showAction
/**
* @Route("/news/{article}",
* name="article_show")
*
* @param Article $article
* @param SlackClient $slack
* @param EntityManagerInterface $entityManager
* @param CommentRepository $commentRepository
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Http\Client\Exception
* @throws \Nexy\Slack\Exception\SlackApiException
*/
public function show( $article,
SlackClient $slack,
EntityManagerInterface $entityManager,
CommentRepository $commentRepository
)
{
$allComments = $commentRepository->findAll();
$comments = $commentRepository->findBy(['article' => $article]);
dump($allComments);die;
$repository = $entityManager->getRepository(Article::class);
/**
* @var Article $article
*/
$articleResult = $repository->findOneBy(['slug' => $article]);
if (!$articleResult) {
throw $this->createNotFoundException('No ' . $article . ' found');
}
Не уверен, если это необходимо но вот моя статья и комментарии, просто чтобы убедиться, что у меня есть что-то здесь.
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
*/
class Article
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @Gedmo\Slug(fields={"title"})
* @ORM\Column(type="string", length=100, unique=true)
*/
private $slug;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $content;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $publishedAt;
/**
* @ORM\Column(type="string", length=255)
*/
private $author;
/**
* @ORM\Column(type="integer")
*/
private $heartCount = 0;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $imageFilename;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="update")
*/
private $updatedAt;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="article")
*/
private $comments;
public function __construct()
{
$this->comments = new ArrayCollection();
}
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 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 getAuthor(): ?string
{
return $this->author;
}
public function setAuthor(string $author): self
{
$this->author = $author;
return $this;
}
public function getHeartCount(): ?int
{
return $this->heartCount;
}
public function setHeartCount(int $heartCount): self
{
$this->heartCount = $heartCount;
return $this;
}
public function getImageFilename(): ?string
{
return $this->imageFilename;
}
public function setImageFilename(?string $imageFilename): self
{
$this->imageFilename = $imageFilename;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): self
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setArticle($this);
}
return $this;
}
public function removeComment(Comment $comment): self
{
if ($this->comments->contains($comment)) {
$this->comments->removeElement($comment);
// set the owning side to null (unless already changed)
if ($comment->getArticle() === $this) {
$comment->setArticle(null);
}
}
return $this;
}
}
и комментарии.
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\CommentRepository")
*/
class Comment
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $authorName;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Article", inversedBy="comments")
* @ORM\JoinColumn(nullable=false)
*/
private $article;
public function getId(): ?int
{
return $this->id;
}
public function getAuthorName(): ?string
{
return $this->authorName;
}
public function setAuthorName(string $authorName): self
{
$this->authorName = $authorName;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getArticle(): ?article
{
return $this->article;
}
public function setArticle(?article $article): self
{
$this->article = $article;
return $this;
}
}
Пожалуйста, дайте мне знать, если вам нужна какая-либо другая информация