Added the Comment entity

This commit is contained in:
Polonkai Gergely 2012-09-15 14:42:14 +02:00
parent 4a61f0a1f1
commit bb9e6e6dba
7 changed files with 289 additions and 7 deletions

View File

@ -0,0 +1,30 @@
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration,
Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your need!
*/
class Version20120915132603 extends AbstractMigration
{
public function up(Schema $schema)
{
// this up() migration is autogenerated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
$this->addSql("CREATE TABLE comments (id INT AUTO_INCREMENT NOT NULL, post_id INT NOT NULL, user_id INT DEFAULT NULL, createdAt DATETIME NOT NULL, subject VARCHAR(150) DEFAULT NULL, hidden TINYINT(1) NOT NULL, content LONGTEXT NOT NULL, INDEX IDX_5F9E962A4B89032C (post_id), INDEX IDX_5F9E962AA76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB");
$this->addSql("ALTER TABLE comments ADD CONSTRAINT FK_5F9E962A4B89032C FOREIGN KEY (post_id) REFERENCES blog_posts (id)");
$this->addSql("ALTER TABLE comments ADD CONSTRAINT FK_5F9E962AA76ED395 FOREIGN KEY (user_id) REFERENCES users (id)");
}
public function down(Schema $schema)
{
// this down() migration is autogenerated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
$this->addSql("DROP TABLE comments");
}
}

View File

@ -0,0 +1,198 @@
<?php
namespace GergelyPolonkai\FrontBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="comments")
*/
class Comment
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Post")
* @ORM\JoinColumn(nullable=false)
*/
private $post;
/**
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(nullable=true)
*/
private $user;
/**
* @ORM\Column(type="datetime", nullable=false)
*/
private $createdAt;
/**
* @ORM\Column(type="string", length=150, nullable=true)
*/
private $subject;
/**
* @ORM\Column(type="boolean", nullable=false)
*/
private $hidden;
/**
* @ORM\Column(type="text", nullable=false)
*/
private $content;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* 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 subject
*
* @param string $subject
* @return Comment
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* Get subject
*
* @return string
*/
public function getSubject()
{
return $this->subject;
}
/**
* Set hidden
*
* @param boolean $hidden
* @return Comment
*/
public function setHidden($hidden)
{
$this->hidden = $hidden;
return $this;
}
/**
* Get hidden
*
* @return boolean
*/
public function getHidden()
{
return $this->hidden;
}
/**
* 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 post
*
* @param GergelyPolonkai\FrontBundle\Entity\Post $post
* @return Comment
*/
public function setPost(\GergelyPolonkai\FrontBundle\Entity\Post $post)
{
$this->post = $post;
return $this;
}
/**
* Get post
*
* @return GergelyPolonkai\FrontBundle\Entity\Post
*/
public function getPost()
{
return $this->post;
}
/**
* Set user
*
* @param GergelyPolonkai\FrontBundle\Entity\User $user
* @return Comment
*/
public function setUser(\GergelyPolonkai\FrontBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return GergelyPolonkai\FrontBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}

View File

@ -6,6 +6,11 @@ use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as GedmoORM; use Gedmo\Mapping\Annotation as GedmoORM;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use GergelyPolonkai\FrontBundle\Entity\Comment;
use GergelyPolonkai\FrontBundle\Entity\User;
/** /**
* Description of Post * Description of Post
* *
@ -73,6 +78,22 @@ class Post
*/ */
private $draft; private $draft;
/**
*
* @var Doctrine\Common\Collections\ArrayCollection $comments
*
* @ORM\OneToMany(targetEntity="Comment", mappedBy="post", fetch="LAZY")
*/
private $comments;
/**
* Constructor
*/
public function __construct()
{
$this->comments = new ArrayCollection();
}
/** /**
* Get id * Get id
* *
@ -122,7 +143,7 @@ class Post
* @param GergelyPolonkai\FrontBundle\Entity\User $user * @param GergelyPolonkai\FrontBundle\Entity\User $user
* @return Post * @return Post
*/ */
public function setUser(\GergelyPolonkai\FrontBundle\Entity\User $user) public function setUser(User $user)
{ {
$this->user = $user; $this->user = $user;
@ -191,7 +212,7 @@ class Post
* @param \DateTime $createdAt * @param \DateTime $createdAt
* @return Post * @return Post
*/ */
public function setCreatedAt($createdAt) public function setCreatedAt(\DateTime $createdAt)
{ {
$this->createdAt = $createdAt; $this->createdAt = $createdAt;
@ -220,4 +241,36 @@ class Post
{ {
return $this->draft; return $this->draft;
} }
/**
* Add comments
*
* @param GergelyPolonkai\FrontBundle\Entity\Comment $comments
* @return Post
*/
public function addComment(Comment $comments)
{
$this->comments[] = $comments;
return $this;
}
/**
* Remove comments
*
* @param GergelyPolonkai\FrontBundle\Entity\Comment $comments
*/
public function removeComment(Comment $comments)
{
$this->comments->removeElement($comments);
}
/**
* Get comments
*
* @return Doctrine\Common\Collections\Collection
*/
public function getComments()
{
return $this->comments;
}
} }

View File

@ -2,7 +2,6 @@
namespace GergelyPolonkai\FrontBundle\Twig; namespace GergelyPolonkai\FrontBundle\Twig;
use JMS\DiExtraBundle\Annotation as DI; use JMS\DiExtraBundle\Annotation as DI;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Description of RandomHeader * Description of RandomHeader
@ -45,10 +44,12 @@ class RandomHeader extends \Twig_Extension
$files[] = $fn; $files[] = $fn;
} }
} }
return $files[array_rand($files)]; return $files[array_rand($files)];
} }
public function getName() { public function getName()
{
return 'random_header'; return 'random_header';
} }
} }