[英]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. comment,post和thread使用类表继承来发布。 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(). 我试图实现的最终目标是能够调用user-> getPosts(),user-> getThreads(),user-> getComments()。 However, I need to fix my orm mapping config because I get mapping errors 但是,我需要修复orm映射配置,因为出现映射错误
The mappings OnlineMovementBundle\\Entity\\User#posts and OnlineMovementBundle\\Entity\\Post#owner are inconsistent with each other. 映射OnlineMovementBundle \\ Entity \\ User#posts和OnlineMovementBundle \\ Entity \\ Post#owner彼此不一致。
The mappings OnlineMovementBundle\\Entity\\User#threads and OnlineMovementBundle\\Entity\\Thread#owner are inconsistent with each other. OnlineMovementBundle \\ Entity \\ User#threads和OnlineMovementBundle \\ Entity \\ Thread#owner的映射彼此不一致。
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;
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. 您不能使用id映射父对象,可以将其作为抽象类,这将是每个孩子的体系结构,这可能是一个好习惯。
Your major mistake is your id definition in parent class and your accessors must be declared as protected (not private) 您的主要错误是您在父类中的id定义,并且访问器必须声明为受保护的(非私有的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.