简体   繁体   English

Symfony 3与类表继承原则的关联Orm 2

[英]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.

相关问题 Symfony - Doctrine Single Table Inheritance ManyToOne 关联与父实体 - Symfony - Doctrine Single Table Inheritance ManyToOne association with parent entity PHP doctrine 1.2 ORM - 具有类表继承的多态查询 - PHP doctrine 1.2 ORM - polymorphic queries with class table inheritance 教义和oneToOne关联和类继承 - Doctrine & oneToOne association & class inheritance 在Doctrine 2中,ManyToOne关联映射到类表继承实体 - ManyToOne association mapping to a Class Table Inheritance entity in Doctrine 2 Symfony / Doctrine类表继承和外键作为主键 - Symfony/Doctrine class table inheritance and foreign key as primary key Symfony2 / Doctrine在类表继承中间映射了超类 - Symfony2 / Doctrine mapped superclass in the middle of class table inheritance 如何删除子表行,但将其父表保留在准则orm 2的“类表继承”中? - How can I delete a child table row but keep its parent in Class Table Inheritance in doctrine orm 2? 在Symfony Doctrine ORM中指定实体类名称 - Specify entity class name in Symfony Doctrine ORM 在Doctrine ORM 2中的类表继承上加入时,“主键elementId的缺失值” - “Missing value for primary key elementId” when joining on Class Table Inheritance in Doctrine ORM 2 Doctrine 2类表继承YML - Doctrine 2 Class Table Inheritance YML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM