【问题标题】:Is it possible to extends a Model with Doctrine 2?是否可以使用 Doctrine 2 扩展模型?
【发布时间】:2010-08-10 14:34:02
【问题描述】:

以下情况:

家长:

namespace Base;

/** @Entity @Table(name="section") */
class Section extends Skeleton {
/** 
 * @Id @Column(type="integer") 
 * @GeneratedValue(strategy="AUTO")
 */
protected $id;

/** @Column(length=256) */
protected $title;

/** @Column(length=256) */
protected $stylesheet;
}

孩子:

namespace Base2;

use \Base\Section AS BaseSection;

/** @Entity @Table(name="tbl_section") */
class Section extends BaseSection {
/**
 * @Id @Column(type="integer")
 * @GeneratedValue(strategy="AUTO")
 */
protected $id;

/** @Column(length=256) */
protected $title;

/** @Column(length=256) */
protected $stylesheet;
}

当我尝试从数据库中检索一个部分时,它会抛出一个错误:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.id' 
in 'where clause' in /var/www/eage_new_zf/library/Doctrine/DBAL/Connection.php 
on line 567 Call Stack #TimeMemoryFunctionLocation 10.0004489704{main}( 
)../index.php:0 20.03193296632Zend_Controller_Front->dispatch( ???, ??? 
)../index.php:27 30.04574505172Zend_Controller_Dispatcher_Standard->dispatch( 
object(Zend_Controller_Request_Http)[39], object(Zend_Controller_Response_Http)[40] 
)../Front.php:954 Variables in local scope (#3) 

它尝试执行的查询是:

SELECT 
    t1.id AS id2, 
    t1.title AS title3, 
    t1.stylesheet AS stylesheet4 
FROM 
    tbl_section t1 
WHERE 
    t0.id = ?

t0 没有定义,所以从技术上讲它是正确的我得到一个错误。但是如何解决这个问题?它是 Doctrine 2 中的错误吗?还是我做错了什么。

【问题讨论】:

    标签: php mysql doctrine mysql-error-1054


    【解决方案1】:

    您可以使用单表或联表继承。不同之处在于要么使用一个带有可为空列的表,要么使用多个表,具体取决于子类。有关 mroe 信息,请参阅手册:

    http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/inheritance-mapping.html

    在你的情况下,在最顶层/根类(Base\Section)

    /**
     * @Entity @Table(name="section")
     * @InheritanceType("SINGLE_TABLE")
     * @DiscriminatorColumn(name="discr", type="string")
     * @DiscriminatorMap({"base" = "Base\Section", "child" = "Base2\Section"})
     */
    

    命名类是一种不好的做法,顺便说一句,您应该根据它们在实现方面所做的事情来命名您的类。即使它重复了已包含在命名空间中的单词,即您的示例中的 Base\BaseSection 和 BAse2\Base2Section。

    【讨论】:

    • 感谢您的回答。我完全错过了你提到的文档,但我想这可以。我不知道我是否同意你的最后一部分,但我猜这只是个人的事情。无论如何感谢您的建议。
    • 链接已失效,这是一个新链接:doctrine-orm.readthedocs.org/en/latest/reference/… ;)
    猜你喜欢
    • 2011-01-20
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    相关资源
    最近更新 更多