user7806384
user7806384

Reputation: 135

Symfony 3 association to class table inheritance Doctrine orm 2

I have the entities user, postable, comment, post, thread. comment, post, and thread use class table inheritance for postable. So each entity that is "postable" has an owner, likes, a report, etc.

The end goal I'm trying to achieve is being able to call user->getPosts(), user->getThreads(), user->getComments(). However, I need to fix my orm mapping config because I get mapping errors

The mappings OnlineMovementBundle\Entity\User#posts and OnlineMovementBundle\Entity\Post#owner are inconsistent with each other.

The mappings OnlineMovementBundle\Entity\User#threads and OnlineMovementBundle\Entity\Thread#owner are inconsistent with each other.

User

class User extends BaseUser
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Postable", mappedBy="owner", cascade={"persist"}, orphanRemoval=true)
 */
private $postables;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Post", mappedBy="owner", cascade={"persist"}, orphanRemoval=true)
 */
private $posts;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Thread", mappedBy="owner", cascade={"persist"}, orphanRemoval=true)
 */
private $threads;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Thread", mappedBy="owner", cascade={"persist"}, orphanRemoval=true)
 */
private $comments;

Postable

/**
 * Postable
 *
 * @ORM\Table(name="postable")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({
 *   "postable" = "Postable",
 *   "post" = "Post",
 *   "thread" = "Thread",
 *   "comment" = "Comment"
 * })
 * @ORM\Entity(repositoryClass="OnlineMovementBundle\Repository\PostableRepository")
 */
class Postable
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToOne(targetEntity="Movement", inversedBy="postables", cascade={"persist"})
 */
private $movement;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToOne(targetEntity="User", inversedBy="postables", cascade={"persist"})
 */
private $owner;

/**
 * @var string
 *
 * @ORM\Column(name="comment", type="string", length=2056)
 */
private $comment;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Report", mappedBy="postable", cascade={"persist"}, orphanRemoval=true)
 */
private $reports;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Bump", mappedBy="postable", cascade={"persist"}, orphanRemoval=true)
 */
private $bumps;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="date_created", type="datetime")
 */
private $dateCreated;

Post

class Post extends Postable
{
/**
 * @var string
 *
 * @Assert\Image(
 *      mimeTypes = {"image/jpeg", "image/gif", "image/png" },
 *      mimeTypesMessage = "upload a jpeg, png, or gif file"
 * )
 *
 * @ORM\Column(name="content", type="string", length=1024, nullable=true)
 */
private $content;

/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=128)
 */
private $title;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Comment", mappedBy="movement", cascade={"persist"}, orphanRemoval=true)
 */
private $comments;

Comment

class Comment extends Postable
{
/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToOne(targetEntity="Post", inversedBy="comments", cascade={"persist"})
 */
private $post;

Thread

class Thread extends Postable
{

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Thread", mappedBy="parent", cascade={"persist"}, orphanRemoval=true)
 */
private $children;

/**
 * @var Thread
 *
 * @ORM\ManyToOne(targetEntity="Thread", inversedBy="children", cascade={"persist"})
 */
private $parent;

Upvotes: 0

Views: 567

Answers (1)

mduvernon
mduvernon

Reputation: 498

You cannot mapping parent with id, you can make it as abstract class which would be the architecture of each of child, that may be a good practice.

Your major mistake is your id definition in parent class and your accessors must be declared as protected (not private)

Upvotes: 0

Related Questions