Bonjour,
Voila je rencontre un petit problème avec mon code.
je veux faire hériter mes entités 'Page' et 'Event' de 'BasePage', en me basant sur l'inheritance Mapping de Doctrine.
Voici mon modèle et mes entités :
modèle BasePage
namespace TO\SiteBundle\Model;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Table(name="base_page")
* @ORM\Entity(repositoryClass="TO\SiteBundle\Repository\PageBaseRepository")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({
* "page" = "Page",
* "event" = "Event",
* })
*/
abstract class BasePage
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
* @Assert\Length(min=10, minMessage="Le titre doit faire au moins {{ limit }} caractères.")
*/
protected $title;
/**
* @var string
*
* @Gedmo\Slug(fields={"title"})
* @ORM\Column(name="slug", type="string", length=255, unique=true)
*/
protected $slug;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
* @Assert\NotBlank()
*/
protected $content;
/**
* @var string
*
* @ORM\Column(name="excerpt", type="text", nullable=true)
*/
protected $excerpt;
/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(name="created_at", type="datetime")
*/
protected $createdAt;
/**
* @Gedmo\Timestampable(on="update")
* @ORM\Column(name="updated_at", type="datetime")
*/
protected $updatedAt;
/**
* @Gedmo\Timestampable(on="change", field={"content"})
* @ORM\Column(name="content_changed_at", type="datetime", nullable=true)
*/
protected $contentChangedAt;
/**
* @ORM\OneToOne(targetEntity="TO\SiteBundle\Entity\Image", cascade={"persist", "remove"})
* @Assert\Valid()
*/
protected $image;
/**
* @ORM\OneToMany(targetEntity="TO\SiteBundle\Entity\AttachedFile", mappedBy="page", orphanRemoval=true, cascade={"persist", "remove"})
*/
protected $attachedFiles;
public function __construct()
{
$this->attachedFiles = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Page
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set content
*
* @param string $content
*
* @return Page
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set excerpt
*
* @param string $excerpt
*
* @return Page
*/
public function setExcerpt($excerpt)
{
$this->excerpt = $excerpt;
return $this;
}
/**
* Get excerpt
*
* @return string
*/
public function getExcerpt()
{
return $this->excerpt;
}
/**
* Set slug
*
* @param string $slug
*
* @return Page
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set image
*
* @param string $image
*
* @return Page
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
*
* @return Page
*/
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 Page
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set contentChangedAt
*
* @param \DateTime $contentChangedAt
*
* @return Page
*/
public function setContentChangedAt($contentChangedAt)
{
$this->contentChangedAt = $contentChangedAt;
return $this;
}
/**
* Get contentChangedAt
*
* @return \DateTime
*/
public function getContentChangedAt()
{
return $this->contentChangedAt;
}
/**
* Add attachedFile
*
* @param \TO\SiteBundle\Entity\AttachedFile $attachedFile
*
* @return Page
*/
public function addAttachedFile(\TO\SiteBundle\Entity\AttachedFile $attachedFile)
{
$this->attachedFiles[] = $attachedFile;
$attachedFile->setPage($this);
return $this;
}
/**
* Remove attachedFile
*
* @param \TO\SiteBundle\Entity\AttachedFile $attachedFile
*/
public function removeAttachedFile(\TO\SiteBundle\Entity\AttachedFile $attachedFile)
{
$this->attachedFiles->removeElement($attachedFile);
}
/**
* Get attachedFiles
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAttachedFiles()
{
return $this->attachedFiles;
}
}
L'entité Page
namespace TO\SiteBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use TO\SiteBundle\Model\BasePage;
/**
* Page
*
* @ORM\Entity()
*/
class Page extends BasePage
{
/**
* @ORM\ManyToMany(targetEntity="TO\SiteBundle\Entity\Category", cascade={"persist"})
*/
protected $categories;
public function __construct()
{
parent::__construct();
$this->categories = new ArrayCollection();
}
/**
* Add category
*
* @param $category
*
* @return Page
*/
public function addCategory(Category $category)
{
$this->categories[] = $category;
return $this;
}
/**
* Remove category
*
* @param $category
*/
public function removeCategory(Category $category)
{
$this->categories->removeElement($category);
}
/**
* Get categories
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
}
l'Entité Event
namespace TO\SiteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use TO\SiteBundle\Model\BasePage;
/**
* Event
*
* @ORM\Entity()
*/
class Event extends BasePage
{
/**
* @ORM\OneToOne(targetEntity="TO\SiteBundle\Entity\Address", cascade={"persist", "remove"})
*/
protected $address;
/**
* @ORM\Column(name="date_start_event", type="datetime", nullable=true)
*/
private $DateStartEvent;
/**
* @ORM\Column(name="date_end_event", type="datetime", nullable=true)
*/
private $DateEndEvent;
/**
* Set dateStartEvent
*
* @param string $dateStartEvent
*
* @return Event
*/
public function setDateStartEvent($dateStartEvent)
{
$this->DateStartEvent = $dateStartEvent;
return $this;
}
/**
* Get dateStartEvent
*
* @return string
*/
public function getDateStartEvent()
{
return $this->DateStartEvent;
}
/**
* Set dateEndEvent
*
* @param string $dateEndEvent
*
* @return Event
*/
public function setDateEndEvent($dateEndEvent)
{
$this->DateEndEvent = $dateEndEvent;
return $this;
}
/**
* Get dateEndEvent
*
* @return string
*/
public function getDateEndEvent()
{
return $this->DateEndEvent;
}
/**
* Set address
*
* @param string $address
*
* @return Event
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return string
*/
public function getAddress()
{
return $this->address;
}
}
Si j'ai bien compris le fonctionnement, je devrais obtenir une seule table avec SINGLE_TABLE ou une table 'base_page' qui référence les 2 autres tables page et event avec JOINED.
Hors, j'obtiens toujours 2 tables : page
et event
, qu'importe le paramètrage utilisé.
Qu'est-ce que je fais mal ?
Merci à vous