My task is (or was) simple: There is a variety but finite types of "Post", each with a vastly different set of properties while all sharing a couple core properties (like: Title, Content, Published, etc)

我的任务很简单:有各种各样但有限类型的“Post”,每个都有一组完全不同的属性,而所有属性都共享一些核心属性(如:Title,Content,Published等)

What I set out to do, was create a base Post entity with all the shared properties (as mentioned above), and have this entity as sort of a "Governor" entity, but more on that in a second. All the different Category entities would extend off of this base Post entity.

我打算做的是创建一个具有所有共享属性的基础Post实体(如上所述),并将此实体作为“Governor”实体的一种,但在一秒内更多。所有不同的类别实体都将延伸到此基本Post实体之外。

I did it this way mostly to appeal to the strengths of the Symfony Form API. There are just way too many fields per different Category type to warrant not doing something a bit automated. With the Form API, I can have Symfony use Doctrine to automatically create the editable fields on a page and populate each field with the acquired value when editing (or blank if unpopulated or new), without me having to individually provide them in HTML.

我这样做主要是为了吸引Symfony Form API的优势。每种不同的类别类型都有太多的字段,以保证不会做一些自动化的事情。使用Form API,我可以让Symfony使用Doctrine自动在页面上创建可编辑字段,并在编辑时使用获取的值填充每个字段(如果未填充或新填充,则填充空白),而不必单独以HTML格式提供。

When I said "Governor" I meant I also wanted the base Post to have it's own Repository, and ideally no associated table. This repository could be used to do certain tasks which is Post Type agnostic, such as a findAll() override that would query each of the Category repositories and merge the results into one result (for example)

当我说“州长”时,我的意思是我也希望基础邮局拥有它自己的存储库,理想情况下没有关联的表。此存储库可用于执行某些与Post Type无关的任务,例如findAll()覆盖,它将查询每个Category存储库并将结果合并为一个结果(例如)

Ideally I just wanted to define the unique property of each category in exactly one location, the Entity, and it would reflect across the entirety of the site. Rather than defining it in the entity, the Edit Form, the front end, table fields, etc.

理想情况下,我只想在一个位置(实体)中定义每个类别的唯一属性,它将反映在整个站点中。而不是在实体中定义它,编辑表单,前端,表字段等。

the problem

It seems to be messing up the built in queries which I have not overridden.

它似乎弄乱了我没有覆盖的内置查询。

consider (please note the bolded text):

考虑(请注意粗体文字):

An exception occurred while executing 'SELECT t1.id AS id2, t1.title AS title3, t1.slug AS slug4, t1.date_added AS date_added5, t1.date_updated AS date_updated6, t1.content AS content7, t1.published AS published8, t1.uid AS uid9, [SENSITIVE FIELDS REDACTED] FROM PostCategoryTest t1 WHERE t0.id = ?' with params ["1"]:

执行'SELECT t1.id AS id2,t1.title AS title3,t1.slug AS slug4,t1.date_added AS date_added5,t1.date_updated AS date_updated6,t1.content AS content7,t1.published AS published8,t1时发生异常.uid AS uid9,[敏感字段已删除]来自PostCategoryTest t1 WHERE t0.id =?'用params [“1”]:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.id' in 'where clause'

SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列't0.id'

This is caused when I call a basic $this->getDoctrine()->getRepository(PostCategoryTest::DoctrinePath)->find(1)

当我调用基本$ this-> getDoctrine() - > getRepository(PostCategoryTest :: DoctrinePath) - > find(1)时会导致这种情况

The source

CommonBundle/Entity/Post.php

<?php

namespace CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Post
 *
 * @ORM\Entity(repositoryClass="CommonBundle\Entity\PostRepository")
 */
abstract class Post
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

    /**
     * @var string
     *
     * @ORM\Column(name="slug", type="string", length=255)
     */
    protected $slug;

    //... and so on and so fourth
}

CommonBundle/Entity/PostCategoryTest.php

<?php

namespace CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 * Post
 *
 * @ORM\Table(options={"comment":"Post subclass"})
 * @ORM\Entity(repositoryClass="CommonBundle\Entity\PostCategoryTestRepository")
 */
class PostCategoryTest extends Post
{
    const DoctrinePath = "CommonBundle:PostCategoryTest";

    // ... all sensitive information redacted, sorry
    // setup just like a standard entity however, nothing special 
}

I do understand there's many more ways to accomplish this, and I may just go ahead and do that as this has provided a few more headaches than I had expected. But for sake of education, I'd like to understand why Doctrine is doing this exactly. This problem never happened when extendsing BaseUser from FOSUserBundle

我知道还有很多方法可以实现这一目标,我可以继续这样做,因为这比我预期的更令人头疼。但是为了教育,我想了解为什么Doctrine正是这样做的。从FOSUserBundle扩展BaseUser时,这个问题从未发生过

1 个解决方案

#1


According to Doctrine's documentation, there are 3 ways to extend a class:

根据Doctrine的文档,有三种扩展类的方法:

  • Mapped superclass: the parent class is not an entity.
  • 映射的超类:父类不是实体。

  • Single table inheritance: one big table for all data (including the extra fields).
  • 单表继承:所有数据的一个大表(包括额外的字段)。

  • Class table inheritance: one main table, plus an extra table for each entity with extra fields.
  • 类表继承:一个主表,以及每个具有额外字段的实体的额外表。

You either want the single table inheritance or the class table inheritance.

您要么是单表继承,要么是类表继承。

The mapped superclass won't work because you want the parent class Post to be a standalone entity. In that case the Post class you should not define it as abstract.

映射的超类将不起作用,因为您希望父类Post是独立实体。在这种情况下,Post类不应该将其定义为抽象类。

Unless you have a large amount of extra fields you should probably use single table inheritance.

除非你有大量的额外字段,否则你应该使用单表继承。

更多相关文章

  1. 装机建项目vs2017和mysql5.7下建项目用EF建实体模型的过程..
  2. MySQL 分区表 partition线上修改分区字段,后续进一步学习partitio
  3. mysql 同一表中.两个字段值互相复制,从一个字段值复制到另一个
  4. MySQL 数据(字段)类型
  5. mysql中逗号分隔字段的更好替代方案
  6. mysql 常用字段和占用 字节数
  7. hibernate(*.hbm.xml)中新添加的字段被标记为红色(找不到)的解决方法
  8. 保持最新的一个字段值,直到它发生变化,然后保持其最新的字段值
  9. 替换wordpress WP_POSTS表中post_date字段的年份(4位数)

随机推荐

  1. android定时器
  2. APP开发实战85-帧动画
  3. Android 导入 aar包引起的Error:Failed t
  4. 在代码中获取Android(安卓)theme中的attr
  5. Android的CTS测试
  6. 探索 Android(安卓)Q:位置权限
  7. android9.0 系统默认时间修改
  8. Android Studio failed to open by givin
  9. 安卓基础到入门学习(复习笔记)
  10. android:shape的使用2