是否可以用原则 2 来扩展模型?

发布于 2024-09-13 12:11:45 字数 1519 浏览 1 评论 0原文

以下情况:

父级:

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 中的错误吗?或者我做错了什么。

Following situation:

Parent:

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;
}

Child:

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;
}

When I try to retrieve an Section from the database it throws an error:

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) 

The query it tries to execute is:

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

t0 is not defined so technically it is correct I get an error. But how to solve this? Is it a bug in Doctrine 2? Or am I doing something wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2024-09-20 12:11:45

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

You can either use Single Table or Joined Table Inheritance. The difference is using either one table with nullable columns, or use many tables depending on the child classes. See the manual for mroe information:

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

In your case on the topmost/root class (Base\Section)

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

Naming classes is a bad practice btw, you should name your classes with regard to what they are doing implementation wise. Even if it duplicates words already included in the namespace, i.e. Base\BaseSection and BAse2\Base2Section in your example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文