【发布时间】:2017-02-20 22:31:29
【问题描述】:
这是我的想法和我的位置。请告诉我这是否是不好的做法!
我有 3 个实体:文章、作者、评论:
关系:
- 文章只有一位作者。
- 文章有很多评论。
- 作者有很多文章。
- 评论只有一位作者。
- 评论有一篇文章。
我创建了一个名为 reviewAction 的控制器,我将在其中构建一个评论表单,将其传递给一个评论视图,并将其嵌入到我的文章显示视图中。
这是我目前的控制器(它不工作)
public function reviewAction(Request $request, Article $article)
{
$reviewForm = $this->createFormBuilder($article);
$reviewForm->add('review')->add('title')
->add('author', AuthorType::class, array("label" => FALSE))
->add('rating', ChoiceType::class, array(
'choices' => array('1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' =>'5'),
'expanded' => true,
'multiple' => false
))
->getForm();
if ($reviewForm->isSubmitted() && $reviewForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('article_show', array('id' => $article->getId()));
}
return $this->render('article/review.html.twig', array(
'form' => $reviewForm->createView(),
));
}
首先,是否建议在此处构建此表单?似乎我在文章控制器中的 Review 类型上工作了很多。不知道我还会在哪里建造它。
我收到此错误:
属性“review”和方法之一“getReview()”、“review()”、“isReview()”、“hasReview()”、“__get()”都不存在并且在类中具有公共访问权限“AppBundle\Entity\Article”。
所以我真的被困住了,如果任何人都可以提供任何东西,我将不胜感激。
编辑
这是我的文章实体
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Article
*
* @ORM\Table(name="article")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ArticleRepository")
* @UniqueEntity(fields={"name"}, message="Note: That article already existed.")
*/
class Article
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, unique=true)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="thumbnail", type="string", length=255, nullable=true)
*/
private $thumbnail;
/**
* @var \DateTime
*
* @ORM\Column(name="created_date", type="datetime")
*/
private $createdDate;
/**
* @ORM\ManyToOne(targetEntity="Author", inversedBy="articles", cascade={"persist"})
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
* @Assert\Valid()
*/
private $author;
/**
* @ORM\OneToMany(targetEntity="Review", mappedBy="article")
*/
private $reviews;
public function __construct()
{
$this->reviews = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Article
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
*
* @return Article
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set thumbnail
*
* @param string $thumbnail
*
* @return Article
*/
public function setThumbnail($thumbnail)
{
$this->thumbnail = $thumbnail;
return $this;
}
/**
* Get thumbnail
*
* @return string
*/
public function getThumbnail()
{
return $this->thumbnail;
}
/**
* Set createdDate
*
* @param \DateTime $createdDate
*
* @return Article
*/
public function setCreatedDate($createdDate)
{
$this->createdDate = $createdDate;
return $this;
}
/**
* Get createdDate
*
* @return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Set authorId
*
* @param integer $authorId
*
* @return Article
*/
public function setAuthorId($authorId)
{
$this->authorId = $authorId;
return $this;
}
/**
* Get authorId
*
* @return int
*/
public function getAuthorId()
{
return $this->authorId;
}
/**
* Set author
*
* @param \AppBundle\Entity\Author $author
*
* @return Article
*/
public function setAuthor(\AppBundle\Entity\Author $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return \AppBundle\Entity\Author
*/
public function getAuthor()
{
return $this->author;
}
/**
* Add review
*
* @param \AppBundle\Entity\Review $review
*
* @return Article
*/
public function addReview(\AppBundle\Entity\Review $review)
{
$this->reviews[] = $review;
return $this;
}
/**
* Remove review
*
* @param \AppBundle\Entity\Review $review
*/
public function removeReview(\AppBundle\Entity\Review $review)
{
$this->reviews->removeElement($review);
}
/**
* Get reviews
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getReviews()
{
return $this->reviews;
}
public function __toString() {
return $this->name;
}
}
这是我的评论实体
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Review
*
* @ORM\Table(name="review")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ReviewRepository")
*/
class Review
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="rating", type="smallint")
*/
private $rating;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity="Author", inversedBy="reviews")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
*/
private $author;
/**
* @ORM\ManyToOne(targetEntity="Article", inversedBy="reviews")
* @ORM\JoinColumn(name="article_id", referencedColumnName="id")
*/
private $article;
/**
* @var string
*
* @ORM\Column(name="review", type="text", nullable=true)
*/
private $review;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set rating
*
* @param integer $rating
*
* @return Review
*/
public function setRating($rating)
{
$this->rating = $rating;
return $this;
}
/**
* Get rating
*
* @return int
*/
public function getRating()
{
return $this->rating;
}
/**
* Set title
*
* @param string $title
*
* @return Review
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set authorId
*
* @param integer $authorId
*
* @return Review
*/
public function setAuthorId($authorId)
{
$this->authorId = $authorId;
return $this;
}
/**
* Get authorId
*
* @return int
*/
public function getAuthorId()
{
return $this->authorId;
}
/**
* Set review
*
* @param string $review
*
* @return Review
*/
public function setReview($review)
{
$this->review = $review;
return $this;
}
/**
* Get review
*
* @return string
*/
public function getReview()
{
return $this->review;
}
/**
* Set author
*
* @param \AppBundle\Entity\Author $author
*
* @return Review
*/
public function setAuthor(\AppBundle\Entity\Author $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return \AppBundle\Entity\Author
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set article
*
* @param \AppBundle\Entity\Author $article
*
* @return Review
*/
public function setArticle(\AppBundle\Entity\Author $article = null)
{
$this->article = $article;
return $this;
}
/**
* Get article
*
* @return \AppBundle\Entity\Author
*/
public function getArticle()
{
return $this->article;
}
}
这是我的文章/review.html.twig 文件
{% extends 'base.html.twig' %}
{% block body %}
<h1>Review Article</h1>
{{ form_start(form) }}
{{ form_widget(form) }}
<input type="submit" value="Create" />
{{ form_end(form) }}
<ul>
<li>
<a href="{{ path('article_index') }}">Back to the list</a>
</li>
</ul>
{% endblock %}
更新:此代码有效
$reviewForm = $this->createFormBuilder($review)
->add('review', ReviewType::class, array("label" => FALSE))
->setAction($this->generateUrl('fundraiser_review', array('id' =>$fundraiser->getId())))
->getForm();
谢谢!
【问题讨论】:
-
不应该是
reviews而不是review吗?尝试阅读错误消息。 -
嗯,@ShiraNai7 你可能正在做一些事情,但现在我对这一切如何运作感到非常困惑。我将其更改为 ->add('reviews') 并且错误消失了。我收到关于标题的错误,所以我将其更改为标题.... 不走运。有没有人可以解释我在这里做错了什么?以及在文章控制器中构建评论表单是否正确?感谢您的评论!
-
哦,我明白为什么“评论”起作用了……我的实体确实有评论属性。所以我还是很困。
-
我的错误来自您的 Twig 文件。您能否在此文件中显示相应的代码:
article/review.html.twig? -
感谢@alvinbunk 的回复。我已经加入了。
标签: php model-view-controller controller symfony