在 Doctrine PHP Symfony 中重写 Doctrine_Record (sfDoctrineRecord) 实例方法
我的背景是 Propel,所以我希望重写 Doctrine_Record (sfDoctrineRecord) 中的神奇 getter 是一件简单的事情,但我要么遇到段错误,要么简单地忽略重写方法,转而使用超类。
https://gist.github.com/697008eaf4d7b606286a
class FaqCategory extends BaseFaqCategory
{
public function __toString()
{
return $this->getCategory();
}
// doesn't work
// override getDisplayName to fall back to category name if getDisplayName doesn't exist
public function getDisplayName() {
// also tried parent::getDisplayName() but got segfault(!)
if(isset($this->display_name)) {
$display_name = $this->display_name;
} else {
$display_name = $this->category;
}
return $display_name;
}
}
扩展/覆盖方法的正确 Doctrine 方法是什么Doctrine_Record 的实例(通过 sfDoctrineRecord 扩展 Doctrine_Record)?这必须是可行的...或者我应该查看模板文档吗?
谢谢, 布莱恩
My background is in Propel, so I was hoping it would be a simple thing to override a magical getter in a Doctrine_Record (sfDoctrineRecord), but I'm getting either a Segfault or the override method is simply ignored in favor of the one in the superclass.
https://gist.github.com/697008eaf4d7b606286a
class FaqCategory extends BaseFaqCategory
{
public function __toString()
{
return $this->getCategory();
}
// doesn't work
// override getDisplayName to fall back to category name if getDisplayName doesn't exist
public function getDisplayName() {
// also tried parent::getDisplayName() but got segfault(!)
if(isset($this->display_name)) {
$display_name = $this->display_name;
} else {
$display_name = $this->category;
}
return $display_name;
}
}
What is the proper Doctrine way to extend/override methods on an instance of Doctrine_Record (via sfDoctrineRecord extends Doctrine_Record)? This has to be doable...or should I be looking at the Template documentation?
Thanks,
Brian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不确定您确切想要做什么,但这里有一些提示:
Doctrine(启用
ATTR_AUTO_ACCESSOR_OVERRIDE
属性,是由 symfony 启用)允许您仅通过在模型类中定义getColumnName
方法来覆盖某些组件列的 getter。这就是为什么您的getDisplayName
方法可能会陷入无限循环,这通常会导致段错误。要直接访问/修改列值(绕过自定义 (get|set) 方法),您必须使用
_get('column_name')
和_set('column_name')
由Doctrine_Record
类定义的方法。所有
$obj->getSomething()
、$obj->something
和$obj['something']
调用其实很神奇。它们被“重定向”到$obj->get('something')
,这是访问模型数据的唯一真实方式。Not sure about what do you wanted to do exactly, but here are some hints:
Doctrine (with
ATTR_AUTO_ACCESSOR_OVERRIDE
attribute enabled, which is enabled by symfony) allows you to override certain component columns' getters just by defininggetColumnName
methods in model class. That's why yourgetDisplayName
method can fall infinite loop which usually causes segfault.To access/modify column value directly (bypassing custom (get|set)ters) you have to use
_get('column_name')
and_set('column_name')
methods defined byDoctrine_Record
class.All the
$obj->getSomething()
,$obj->something
and$obj['something']
calls are actually magical. They are "redirected" to$obj->get('something')
which is only real way to access model data.尝试 _get 和 _set 方法。
Try _get and _set methods.
这有效:
This works:
配置 Dotrine:
然后:
Configure Doctrine:
then: