Skip to content

Instantly share code, notes, and snippets.

@frodosghost
Created April 13, 2012 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frodosghost/2374321 to your computer and use it in GitHub Desktop.
Save frodosghost/2374321 to your computer and use it in GitHub Desktop.
BasePage setup for Doctrine2 Table Inheritance. Shows Content from same bundle and FrontPage from a separate Bundle.
<?php
namespace Manhattan\Bundle\ContentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Manhattan\Bundle\ContentBundle\Entity\BasePage
*
* @ORM\Table(name="base_page")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="class_name", type="string")
* @ORM\DiscriminatorMap({
* "content" = "Content",
* "other_content" = "Manhattan\Bundle\OtherBundle\Entity\FrontPage"
* })
*/
abstract class BasePage
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $meta_description
*
* @ORM\Column(name="meta_description", type="string", length=500)
*/
private $meta_description;
/**
* @var string $html_title
*
* @ORM\Column(name="html_title", type="string", length=255)
*/
private $html_title;
/**
* @var datetime $created_at
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $created_at;
/**
* @var datetime $updated_at
*
* @ORM\Column(name="updated_at", type="datetime")
*/
private $updated_at;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set meta_description
*
* @param string $metaDescription
*/
public function setMetaDescription($metaDescription)
{
$this->meta_description = $metaDescription;
}
/**
* Get meta_description
*
* @return string
*/
public function getMetaDescription()
{
return $this->meta_description;
}
/**
* Set html_title
*
* @param string $htmlTitle
*/
public function setHtmlTitle($htmlTitle)
{
$this->html_title = $htmlTitle;
}
/**
* Get html_title
*
* @return string
*/
public function getHtmlTitle()
{
return $this->html_title;
}
/**
* Set created_at
*
* @param datetime $createdAt
*/
public function setCreatedAt(\DateTime $createdAt)
{
$this->created_at = $createdAt;
}
/**
* Get created_at
*
* @return datetime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* Set updated_at
*
* @param datetime $updatedAt
*/
public function setUpdatedAt(\DateTime $updatedAt)
{
$this->updated_at = $updatedAt;
}
/**
* Get updated_at
*
* @return datetime
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
/**
* @orm:prePersist
*/
public function prePersist() {
$this->setCreatedAt(new \DateTime());
$this->setUpdatedAt(new \DateTime());
}
/**
* @orm:preUpdate
*/
public function preUpdate() {
$this->setUpdatedAt(new \DateTime());
}
}
<?php
namespace Manhattan\Bundle\ContentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Manhattan\Bundle\ContentBundle\Entity\Content
*
* @ORM\Table(name="content")
* @ORM\Entity(repositoryClass="Manhattan\Bundle\ContentBundle\Entity\ContentRepository")
*/
class Content extends Page
{
/**
* @var string $title
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var text $body
*
* @ORM\Column(name="body", type="text")
*/
private $body;
/**
* Set title
*
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set body
*
* @param text $body
*/
public function setBody($body)
{
$this->body = $body;
}
/**
* Get body
*
* @return text
*/
public function getBody()
{
return $this->body;
}
}
<?php
namespace Manhattan\Bundle\OtherBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Manhattan\Bundle\ContentBundle\Entity\BasePage;
/**
* Manhattan\Bundle\OtherBundle\Entity\FrontPage
*
* @ORM\Table(name="front_page")
* @ORM\Entity(repositoryClass="Manhattan\Bundle\OtherBundle\Entity\FrontPageRepository")
*/
class FrontPage extends BasePage
{
/**
* @var string $title
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* Set title
*
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment