【发布时间】:2015-05-13 01:21:20
【问题描述】:
我的任务很简单(或曾经)很简单:“帖子”有多种但有限的类型,每种类型都有截然不同的属性,同时都共享几个核心属性(例如:标题、内容、已发布等)
我打算做的是创建一个具有所有共享属性的基础Post 实体(如上所述),并将该实体作为一种“治理器”实体,但稍后会详细介绍。所有不同的Category 实体都将从这个基础 Post 实体扩展。
我这样做主要是为了利用 Symfony Form API 的优势。每个不同的 Category 类型都有太多的字段来保证不做一些自动化的事情。使用 Form API,我可以让 Symfony 使用 Doctrine 在页面上自动创建可编辑字段,并在编辑时使用获取的值填充每个字段(如果未填充或新填充则为空白),而无需我在 HTML 中单独提供它们。
当我说“Governor”时,我的意思是我还希望基础 Post 拥有它自己的存储库,理想情况下没有关联的表。此存储库可用于执行与 Post Type 无关的某些任务,例如 findAll() 覆盖,它将查询每个 Category 存储库并将结果合并为一个结果(例如)
理想情况下,我只想在一个位置(实体)中定义每个类别的唯一属性,它会反映在整个网站中。而不是在实体、编辑表单、前端、表格字段等中定义。
问题
它似乎弄乱了我没有覆盖的内置查询。
考虑(请注意粗体字):
执行 '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, [敏感字段已编辑] FROM PostCategoryTest t1 WHERE t0.id = ?' with params ["1"]:
SQLSTATE[42S22]:未找到列:1054 中的未知列 't0.id' 'where子句'
这是我打电话给基本的$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 为什么会这样做。当extendsing BaseUser from FOSUserBundle 时,这个问题从未发生过
【问题讨论】:
-
你为什么要这么做
const DoctrinePath = "CommonBundle:PostCategoryTest";?与问题无关 -
@ThomasPotaire 你是对的,我只是忽略了从代码中删除它,我只强调删除敏感或冗余行(getter/setter、不相关的属性等)
标签: php mysql symfony doctrine-orm