【问题标题】:Transparent change value of return value via ORM通过 ORM 透明改变返回值的值
【发布时间】:2014-06-25 09:15:35
【问题描述】:

我有 ORM 模型硬件。并且硬件与 Type ORM 模型有关。 我需要从硬件模型中获取选项,例如:

$hardware->option 

但如果选项为空,我想从类型模型中获取选项:

$hardware->type->option

我想以硬件形式自动(透明)进行。我做到了:

public function __get($column) {

    if($column == 'option') {

        if ($this->option != null) {
            return $this->option;
        } else {
            // if option equals null then use value from type of this hardware
            return $this->type->captive_portal;
        }
    }

    return parent::__get($column);
}

但它不起作用。因为 $this->option 不起作用,因为首先我需要调用父 __get 方法来进行关系工作。但我不能调用父方法 __get 并在它获得真正的价值之后。还是可以?

【问题讨论】:

    标签: php oop orm model kohana


    【解决方案1】:

    效果很好;)

    public function __get($column) {
        if($column == 'option') {
            return $this->getOption( parent::__get($column) );
        } else {
            return parent::__get($column);
        }
    }
    
    public function getOption($thisValue) {
    
        if ($thisValue != null) {
            return $thisValue;
        } else {
            // if option equals null then use value from type of this hardware
            return $this->type->option;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      相关资源
      最近更新 更多