I successfuly created working bundle for my blog, including administration of articles, but I'm stuck with Comments. I want to separate comments by user and anonymous user. If user is logged, then he will not see field for Author and he will not see captcha. I think, that this problem I can solve with one if in the form builder (if user is fully authenticated) and then in my TWIG template too. But, it is the good solution? Isn't there any easier way? So my Comment.php will have this form:
<?php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="comments")
*/
class Comment
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=200)
* @Assert\NotBlank(
* message = "Name cannot be blank"
* )
* @Assert\Length(
* min = "3",
* minMessage = "Name is too short"
* )
*/
private $author;
/**
* @ORM\ManyToOne(targetEntity="\Acme\UserBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user_id;
/**
* @ORM\Column(type="string", length=200)
* @Assert\NotBlank(
* message = "E-mail cannot be blank"
* )
* @Assert\Length(
* min = "3",
* minMessage = "E-mail is too short"
* )
*/
private $email;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank(
* message = "Message cannot be blank"
* )
* @Assert\Length(
* min = "3",
* minMessage = "Message is too short"
* )
*/
private $content;
/**
* @ORM\ManyToOne(targetEntity="Article", inversedBy="comments")
* @ORM\JoinColumn(referencedColumnName="id")
*/
private $article;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set author
*
* @param string $author
* @return Comment
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set email
*
* @param string $email
* @return Comment
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* 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 article
*
* @param \Acme\BlogBundle\Entity\Article $article
* @return Comment
*/
public function setArticle(\Acme\BlogBundle\Entity\Article $article = null)
{
$this->article = $article;
return $this;
}
/**
* Get article
*
* @return \Acme\BlogBundle\Entity\Article
*/
public function getArticle()
{
return $this->article;
}
/**
* Set user_id
*
* @param \Acme\UserBundle\Entity\User $userId
* @return Comment
*/
public function setUserId(\Acme\UserBundle\Entity\User $userId = null)
{
$this->user_id = $userId;
return $this;
}
/**
* Get user_id
*
* @return \Acme\UserBundle\Entity\User
*/
public function getUserId()
{
return $this->user_id;
}
This was my first problem, then my second problem I see with displaying comments (rather author name). I'm not sure, how to create a new value into my $comments
property in my Controller (if property user_id
is not NULL, then load from User object information about this user with this ID, and if it is NULL, then use author
property. And if is comment by User, the author name will be underlined. Also, it is work for my Controller or I can do it in my TWIG template?
My questions in short:
- Which is the best method displaying "Write your comment" form build with logged and not-logged users (most effetive, not sure using
if
in my form builder and in my TWIG template) - How to separate comments by anonymous/registered users and if is comment by registered user, his name will be underlined