Overriding Doctrine_Record (sfDoctrineRecord) instance methods in Doctrine PHP Symfony

点点圈 提交于 2019-12-04 10:53:23

问题


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


回答1:


Try _get and _set methods.




回答2:


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.




回答3:


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;

  }

}



回答4:


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


来源:https://stackoverflow.com/questions/2587115/overriding-doctrine-record-sfdoctrinerecord-instance-methods-in-doctrine-php-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!