在 Doctrine PHP Symfony 中重写 Doctrine_Record (sfDoctrineRecord) 实例方法

发布于 2024-08-28 01:39:41 字数 915 浏览 9 评论 0原文

我的背景是 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 技术交流群。

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

发布评论

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

评论(4

我家小可爱 2024-09-04 01:39:41

不确定您确切想要做什么,但这里有一些提示:

  1. Doctrine(启用 ATTR_AUTO_ACCESSOR_OVERRIDE 属性,由 symfony 启用)允许您仅通过在模型类中定义 getColumnName 方法来覆盖某些组件列的 getter。这就是为什么您的 getDisplayName 方法可能会陷入无限循环,这通常会导致段错误。

  2. 要直接访问/修改列值(绕过自定义 (get|set) 方法),您必须使用 _get('column_name')_set('column_name')Doctrine_Record 类定义的方法。

  3. 所有 $obj->getSomething()$obj->something$obj['something'] 调用其实很神奇。它们被“重定向”到 $obj->get('something'),这是访问模型数据的唯一真实方式。

Not sure about what do you wanted to do exactly, but here are some hints:

  1. Doctrine (with ATTR_AUTO_ACCESSOR_OVERRIDE attribute enabled, which is enabled by symfony) allows you to override certain component columns' getters just by defining getColumnName methods in model class. That's why your getDisplayName method can fall infinite loop which usually causes segfault.

  2. To access/modify column value directly (bypassing custom (get|set)ters) you have to use _get('column_name') and _set('column_name') methods defined by Doctrine_Record class.

  3. 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.

左耳近心 2024-09-04 01:39:41

尝试 _get 和 _set 方法。

Try _get and _set methods.

小傻瓜 2024-09-04 01:39:41

这有效:

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;

  }

}

This works:

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;

  }

}
可爱暴击 2024-09-04 01:39:41

配置 Dotrine:

$manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true); 

然后:

public function getAnything()
{
    return $this->_get('anything');
}

public function setAnything()
{
    return $this->_set('anything', $value);
}

Configure Doctrine:

$manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true); 

then:

public function getAnything()
{
    return $this->_get('anything');
}

public function setAnything()
{
    return $this->_set('anything', $value);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文