PHP ActiveRecord 似乎没有使用我的自定义属性


PHP ActiveRecord doesn't seem to use my custom attribute

我有一个模型定义如下:

class User extends ActiveRecord'Model {
  function get_name() {
    return $this->first_name . " " . $this->surname;
  }
}

但是当我显示$item->attributes();时,名称不会出现。我在这里是个白痴吗?如果是这样,如何将我的自定义属性导入模型?

谢谢加雷思

这是我的简单解决方案。我已经覆盖了属性方法并添加了任何以"get_attribute_"开头的方法,以便我可以在序列化期间使用它们:

class BaseModel extends ActiveRecord'Model
{
    public function attributes()
    {
        $attrs = parent::attributes();
        $modelReflector = new ReflectionClass(get_class($this));
        $methods = $modelReflector->getMethods(~ReflectionMethod::IS_STATIC & ReflectionMethod::IS_PUBLIC);
        foreach ($methods as $method)
        {
            if (preg_match("/^get_attribute_/", $method->getName()))
            {
                $attrs[str_replace('get_attribute_', '', $method->getName())] = $method->invoke($this);
            }   
        }
        return $attrs;
    }
}

使用它的结果模型如下所示:

class User extends BaseModel
{
    public $loginSessionId;
    function get_attribute_loginSessionId(){
        return $this->loginSessionId;
    }
}

这样,我可以手动附加loginSessionId(或我想要的任何其他内容),并使其显示在序列化值中。

attributes() 方法确实只会返回模型表列的值(不锯齿)。

但是$item->name应该给你预期的结果。您还可以添加二传手。

若要获取所有属性的数组,可以将此方法添加到模型中:

public function all_attributes() {
    $custom_attr = [];
    foreach (static::$getters as $getter) {
        $key = substr($getter, 4);
        $custom_attr[$key] = $this->$key;
    }
    return $custom_attr + $this->attributes();
}

(不要忘记将您的 getter 添加到 $getters 数组中,ActiveRecord 模型使用它)

您必须检查属性函数的作用。您可能需要覆盖它以考虑您的自定义属性,或者您可能必须将自定义属性添加到祖先中的某个_properties属性

你能告诉我们你是如何创造$item的吗?PHPActiveRecord 需要你在构造函数调用中设置属性 ( new User($attributes) ) 或直接在属性 ( $user->first_name = 'Gareth' 上 )。


编辑

我不确定属性()方法是否会选择自定义getter。似乎它只返回模型的属性属性。

https://github.com/kla/php-activerecord/blob/master/lib/Model.php#L566

我按如下方式解决了这个问题:

创建一个派生自ActiveRecord'Model的类并包含以下函数:

public function properties()
{
  $attrs = $this->attributes();
  $modelReflector = new ReflectionClass(get_class($this));
  $methods = $modelReflector->getMethods(~ReflectionMethod::IS_STATIC & ReflectionMethod::IS_PUBLIC);
  foreach ($methods as $method)
  {
    if (preg_match("/^get_/", $method->getName()))
    {
      $attrs[str_replace('get_', '', $method->getName())] = $method->invoke($this);
    }
  }
  return $attrs;
}

这将返回所有属性,自定义或其他属性,只要我从正确的类(而不是ActiveRecord'Model)派生我的模型。

实际上,它很容易实现,就像这样@ 将此添加到 https://github.com/kla/php-activerecord/blob/master/lib/Model.php#L520:

// check for attribute as getter
if ( method_exists( $this, "get_{$name}" ) ){
  $method = "get_{$name}";
  $var = $this->$method();
  return $var;
}

但我更喜欢这样做(更干净/优化的代码):

某模.php:

  /** 
   * gets templatefile of outputplugin, wrapper function for template comfortability 
   * 
   * NOTE 2: never implement functions in model, try redirecting
   * to manager class (=lowmemory footprint, imagine xxxxx models with xxxx similar functions)
   *
   * @param string $varname variable description 
   * @return string 
   */ 
  public function get( $varname )
  { 
    switch( $varname ){
      case "foo" : return SomeModel::getManager()->getFoo(); break;
      default: return "";
    }
  }

将此添加到 https://github.com/kla/php-activerecord/blob/master/lib/Model.php#L520:

// check for attribute as getter
if ( method_exists( $this, "get" ) && $this->get( $name ) ){ 
  $var = $this->get( $name );
  return $var;
}