Bri*_*ian 8 php doctrine symfony1 extending getter-setter
我的背景是在Propel中,所以我希望在Doctrine_Record(sfDoctrineRecord)中覆盖一个神奇的getter是一件简单的事情,但是我得到了一个Segfault或者覆盖方法被简单地忽略了,而不是超类.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在Doctrine_Record实例上扩展/覆盖方法的正确Doctrine方法是什么(通过sfDoctrineRecord扩展Doctrine_Record)?这必须是可行的......或者我应该查看模板文档?
谢谢,Brian
不确定你想要做什么,但这里有一些提示:
学说(带ATTR_AUTO_ACCESSOR_OVERRIDE属性启用,这是由symfony中启用)允许您只要定义替换某些组件列干将getColumnName模型类的方法.这就是为什么你的getDisplayName方法可能会陷入无限循环,这通常会导致段错误.
要直接访问/修改列值(绕过自定义(get | set)ters),必须使用类_get('column_name')和类_set('column_name')定义的方法Doctrine_Record.
所有的$obj->getSomething(),$obj->something和$obj['something']电话实际上是神奇的.它们被"重定向",$obj->get('something')这是访问模型数据的真正方式.
这有效:
class FaqCategory extends BaseFaqCategory
{
public function __toString()
{
return $this->getCategory();
}
public function getDisplayName() {
if($this->_get("display_name") != "") {
$display_name = $this->_get("display_name");
} else {
$display_name = $this->getCategory();
}
return $display_name;
}
}
Run Code Online (Sandbox Code Playgroud)