教义将带有扩展的实体的查询弄乱了 [英] Doctrine messing up queries on Entities with an Extends

查看:47
本文介绍了教义将带有扩展的实体的查询弄乱了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务很简单(或曾经很简单):发帖"种类繁多但数量有限,每种都有非常不同的属性集,而它们都共享几个核心属性(例如:标题,内容,已发布等)

我打算做的是创建一个具有所有共享属性的基本 Post 实体(如上所述),并将该实体作为总督"实体进行分类,但有关更多内容,请参见一秒钟.所有不同的 Category 实体都将从该基本Post实体扩展而来.

我这样做的主要目的是吸引Symfony Form API的优势.每种不同类别类型的字段太多,以至于不能做一些自动化的事情.借助Form API,我可以让Symfony使用Doctrine在页面上自动创建可编辑字段,并在编辑时(如果未填充或新建则为空白)用获取的值填充每个字段,而无需我单独以HTML形式提供它们.

当我说总督"时,我的意思是我还希望基本Post具有自己的存储库,理想情况下没有关联的表.此存储库可用于执行某些与Post Type无关的任务,例如 findAll()覆盖,该查询将查询每个Category存储库并将结果合并为一个结果(例如)

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

问题

这似乎弄乱了我没有覆盖的内置查询.

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

执行'SELECT t1.id AS id2,t1.title时发生异常AS标题3,t1.slug AS slug4,t1.date_added AS date_added5,t1.date_updated AS date_updated6,t1.content AS content7,t1.publishedAS出版8,t1.uid AS uid9,从 [已删除敏感字段] PostCategoryTest t1,其中带参数["1"]的 t0.id =?':

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

这是当我调用基本的 $ this-> getDoctrine()-> getRepository(PostCategoryTest :: DoctrinePath)-> find(1)

时引起的

来源

CommonBundle/Entity/Post.php

 <?php命名空间CommonBundle \ Entity;使用Doctrine \ ORM \ Mapping作为ORM;/*** 邮政** @ORM \ Entity(repositoryClass ="CommonBundle \ Entity \ PostRepository")*/抽象类帖子{/*** @var整数** @ORM \ Column(name ="id",type ="integer")* @ORM \ Id* @ORM \ GeneratedValue(strategy ="AUTO")*/受保护的$ id;/*** @var字符串** @ORM \ Column(name ="title",type ="string",length = 255)*/受保护的$ title;/*** @var字符串** @ORM \ Column(name ="slug",type ="string",length = 255)*/受保护的$ slug;//...依此类推} 

CommonBundle/Entity/PostCategoryTest.php

 <?php命名空间CommonBundle \ Entity;使用Doctrine \ ORM \ Mapping作为ORM;/*** 邮政** @ORM \ Table(options = {"comment":"Post subclass"})* @ORM \ Entity(repositoryClass ="CommonBundle \ Entity \ PostCategoryTestRepository")*/PostCategoryTest类扩展了Post{const DoctrinePath ="CommonBundle:PostCategoryTest";//...所有敏感信息均已删除,对不起//设置就像标准实体一样,没什么特别的} 

我确实知道还有更多方法可以完成此操作,我可能会继续这样做,因为这样做比我预期的要麻烦得多.但是为了教育起见,我想了解为什么Doctrine正是这样做的.从FOSUserBundle中的扩展扩展 BaseUser 时,从未发生此问题

解决方案

根据 Doctrine的文档,可以通过3种方式扩展类:

您要单表继承还是类表继承.

映射的超类将无法工作,因为您希望父类 Post 为独立实体.在这种情况下,您不应该将 Post 类定义为抽象.

除非您有大量额外的字段,否则您可能应该使用单表继承.

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)

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.

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.

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)

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"]:

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

This is caused when I call a basic $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

解决方案

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

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.

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

这篇关于教义将带有扩展的实体的查询弄乱了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆