Propel 无法创建具有继承的具体类的实例?
我找不到在 Propel 中使用带有单一继承和抽象父类的 findOrCreate()
的方法。这个小例子给了我一个致命错误:
致命错误:无法实例化抽象类 ComicBook propel\query\ModelCriteria.php 第 1181 行
抛出这个致命错误的查询相当简单:
$thebook = ComicBookQuery::create()->filterById(1)->findOneOrCreate();
一小部分数据库模式:
<table name="book" abstract="true" phpName="AbstractBook">
<column name="id" type="INTEGER" required="true"
primaryKey="true" autoIncrement="true"/>
<column name="type" type="VARCHAR" size="255" required="true"
inheritance="single">
<inheritance key="Abstract" class="AbstractBook"/>
<inheritance key="ComicBook" class="ComicBook" extends="AbstractBook"/>
</column>
</table>
有没有办法让继承工作与抽象父类?
I can't find a way to use findOrCreate()
with single inheritance and abstract parent class in Propel. This little example gives me a fatal error:
Fatal error: Cannot instantiate abstract class ComicBook
propel\query\ModelCriteria.php on line 1181
The query that throws this fatal error is fairly simple:
$thebook = ComicBookQuery::create()->filterById(1)->findOneOrCreate();
A little piece of database schema:
<table name="book" abstract="true" phpName="AbstractBook">
<column name="id" type="INTEGER" required="true"
primaryKey="true" autoIncrement="true"/>
<column name="type" type="VARCHAR" size="255" required="true"
inheritance="single">
<inheritance key="Abstract" class="AbstractBook"/>
<inheritance key="ComicBook" class="ComicBook" extends="AbstractBook"/>
</column>
</table>
Is there any way to get inheritance working with abstract parent class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单表继承的主要问题是你总是伪造东西。您收到此错误是因为真正的模型是
AbstractBook
,没有别的...使用
abstract=false
,您将能够实例化AbstractBook
> (这根本就不是抽象的……),然后它将被转换成ComicBook
。通过设置
abstract=true
,您无法实例化AbstractBook
。压倒一切来救援!我没有深入研究代码,但是,如果您想使用
findOneOrCreate()
创建ComicBook
,则必须重写以下方法:getModelName()
在您的ComicBookQuery
类中:最好重写方法以满足您的需求;)
The main issue with the single table inheritance is that you always fake things. You get this error because the real model is
AbstractBook
, nothing else...With
abstract=false
, you'll be able to instanciate anAbstractBook
(which won't be abstract at all…), and then it will be transformed into aComicBook
.By setting
abstract=true
, you cannot instantiate anAbstractBook
. Overriding to the rescue!I didn't dig a lot in the code but, if you want to create
ComicBook
withfindOneOrCreate()
, you'll have to override the following method:getModelName()
in yourComicBookQuery
class:It's a best practice to override methods to fit your needs ;)