代码之家  ›  专栏  ›  技术社区  ›  Rene Terstegen

有没有可能用条令2扩展一个模型?

  •  1
  • Rene Terstegen  · 技术社区  · 14 年前

    以下情况:

    起源:

    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没有定义,所以技术上是正确的,我得到一个错误。但是如何解决这个问题呢?这是条令2中的错误吗?或者我做错了什么。

    1 回复  |  直到 12 年前
        1
  •  2
  •   benlumley    12 年前

    可以使用单表继承或联接表继承。区别在于,要么使用一个列可以为空的表,要么根据子类使用多个表。有关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"})
     */
    

    命名类是一个糟糕的实践btw,您应该根据类的实现方式来命名它们。即使它复制了名称空间中已经包含的单词,例如示例中的base\basesection和base2\base2section。

    推荐文章