我的任务很简单:有各种各样但类型有限的“帖子”,每一种都有一组截然不同的属性,但都共享几个核心属性(比如:标题、内容、发布等)。
我要做的是创建一个具有所有共享属性的基本Post
实体(如上所述),并将此实体作为某种“州长”实体,但稍后将对此进行更多介绍。所有不同的Category
实体都将从这个基本Post实体扩展出来。
我这样做主要是为了吸引Symfony Form API的优势。每个不同的Category类型都有太多的字段,不能保证不做一些自动化的事情。通过表单API,我可以让Symfony使用Doctrine在页面上自动创建可编辑字段,并在编辑时使用获取的值填充每个字段(如果未填充或新的,则为空),而不必在HTML中单独提供它们。
当我说“管理员”的时候,我的意思是我也希望基础帖子有它自己的存储库,理想情况下没有关联表。此存储库可用于执行某些与发布类型无关的任务,例如查询每个类别存储库并将结果合并为一个结果的findAll()
覆盖(例如)
理想情况下,我只想在一个位置定义每个类别的唯一属性,即实体,它将反映整个站点。而不是在实体、编辑窗体、前端、表字段等中定义它。
the problem
它似乎打乱了我没有覆盖的内置查询。
请考虑(请注意粗体文本):
执行'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.Publed AS published8,t1.uid AS uid9,敏感字段从PostCategoryTest t1 WHERE t0.id = ?'编辑时出现异常,参数为"1":
SQLSTATE42S22:找不到列: 1054“where子句”中的未知列“t0.id”
这是在我调用一个基本的$this->getDoctrine()->getRepository(PostCategoryTest::DoctrinePath)->find(1)
时引起的
源
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
}
我知道有很多方法可以做到这一点,我可能会继续这样做,因为这提供了比我预期更多的令人头疼的问题。但为了教育,我想了解一下Doctrine到底为什么要这么做。从FOSUserBundle对BaseUser
执行extends
操作时从未发生过此问题
发布于 2015-05-13 10:07:56
根据Doctrine's documentation的说法,有3种方法可以扩展一个类:
您要么想要单表继承,要么想要类表继承。
映射的超类将不起作用,因为您希望父类Post
成为独立的实体。在这种情况下,您不应该将Post
类定义为abstract
。
除非您有大量的额外字段,否则您可能应该使用single table inheritance
。
https://stackoverflow.com/questions/30204048
复制相似问题