访问Symfony博客

时间:2017-07-07 12:53:19

标签: php symfony symfony-3.2

我正在开发一个实现公共Feed的项目。我有一个Comment实体,允许用户向每个Feed提交评论。但是,我希望用户也能够提交对评论的回复。

为此,我在$response实体中拥有Comment属性。我假设保存响应与保存注释相同,因为响应也是注释。

但是,我不确定如何在没有id的情况下访问评论并保存该评论的回复。

在我的FeedController我保存了这样的评论;

 $commentForm = $this->createForm(CommentType::class);
    $commentForm->handleRequest($request);
    if ($commentForm->isSubmitted() && $commentForm->isValid()) {
        $content = $commentForm->get('content')->getData();
        $feed_id = $feed;

        $comment= EntityBuilder::newComment($content, $user, $feed_id);

        $commentService->saveEntity($comment);

        if(!is_null($comment)){
            $this->addFlash(
                'commentsuccess',
                'Your reply was successfully posted!'
            );
        }

        return $this->redirect($this->generateUrl('showFeed', array('slug' => $feed->getSlug()), UrlGeneratorInterface::ABSOLUTE_URL));

    }

这是评论实体;

/**
* Class Comment
* @package AppBundle\Entity
* @ORM\Entity(repositoryClass="AppBundle\Repository\CommentRepository")
* @ORM\Table(name="comment")
* @ORM\HasLifecycleCallbacks()
*
*/
class Comment
{

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

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Feed", inversedBy="comments")
 * @ORM\JoinColumn(name="feedid", referencedColumnName="id")
 */
private $feedid;

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="comments")
 * @ORM\JoinColumn(name="createdby", referencedColumnName="id")
 */
private $createdby;

/**
 * @ORM\Column(type="string")
 * @Assert\NotBlank(message="Please fill in the description")
 */
private $content;

/**
 * @ORM\OneToOne(targetEntity="Comment")
 */
protected $response;


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

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


/**
 * Constructor
 */
public function __construct()
{
    $this->createdAt= new \DateTime();
    $this->updatedAt= new \DateTime();
}

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set content
 *
 * @param string $content
 *
 * @return Comment
 */
public function setContent($content)
{
    $this->content = $content;

    return $this;
}

/**
 * Get content
 *
 * @return string
 */
public function getContent()
{
    return $this->content;
}

/**
 * Set feedid
 *
 * @param \AppBundle\Entity\Feed $feedid
 *
 * @return Comment
 */
public function setFeedid(\AppBundle\Entity\Feed $feedid = null)
{
    $this->feedid = $feedid;

    return $this;
}

/**
 * Get feedid
 *
 * @return \AppBundle\Entity\Feed
 */
public function getFeedid()
{
    return $this->feedid;
}

/**
 * Set createdby
 *
 * @param \AppBundle\Entity\User $createdby
 *
 * @return Comment
 */
public function setCreatedby(\AppBundle\Entity\User $createdby = null)
{
    $this->createdby = $createdby;

    return $this;
}

/**
 * Get createdby
 *
 * @return \AppBundle\Entity\User
 */
public function getCreatedby()
{
    return $this->createdby;
}

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 *
 * @return Comment
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

/**
 * Set updatedAt
 *
 * @param \DateTime $updatedAt
 *
 * @return Comment
 */
public function setUpdatedAt($updatedAt)
{
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return \DateTime
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}


/**
 * Set response
 *
 * @param \AppBundle\Entity\Comment $response
 *
 * @return Comment
 */
public function setResponse(\AppBundle\Entity\Comment $response = null)
{
    $this->response = $response;

    return $this;
}

/**
 * Get response
 *
 * @return \AppBundle\Entity\Comment
 */
public function getResponse()
{
    return $this->response;
}

}

即使我为响应创建了另一种表单类型,我仍然不确定如何保存特定注释的响应。

1 个答案:

答案 0 :(得分:1)

我的推荐是实施父/子方法,向ManyToOne实体添加($parentComment字段及其反转 (OneToMany$children属性。稍后,您可以在CollectionType属性的CommentType中添加$children表单字段,因此每条评论可能需要多个响应。

不要担心父idCollectionType会为你工作。 详细了解CollectionType的工作原理glyph EURO SIGN, code point U+20AC