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

PHP ActiveRecord没有';我似乎没有使用我的自定义属性,php,activerecord,phpactiverecord,Php,Activerecord,Phpactiverecord,我有一个定义如下的模型: class User extends ActiveRecord\Model { function get_name() { return $this->first_name . " " . $this->surname; } } 但是当我显示$item->attributes()时则不显示名称。我是个白痴吗?如果是,如何将自定义属性放入模型中 谢谢, Gareth您必须检查attributes函数的功能。 您可能需要覆盖它以考虑您的自定义属

我有一个定义如下的模型:

class User extends ActiveRecord\Model {
  function get_name() {
    return $this->first_name . " " . $this->surname;
  }
}
但是当我显示
$item->attributes()时则不显示名称。我是个白痴吗?如果是,如何将自定义属性放入模型中

谢谢,
Gareth

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

您能告诉我们您是如何创建$item的吗?PHPActiveRecord需要您在构造函数调用(
新用户($attributes)
)中设置属性,或者直接在属性(
$User->first\u name='Gareth'
)上设置属性


编辑

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

attributes()方法实际上只返回模型表列的值(不带别名)

但是
$item->name
应该会给出预期的结果。您还可以添加setter

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

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添加到$getter数组中,ActiveRecord模型将使用它)

我解决了这个问题,如下所示:

class User extends ActiveRecord\Model {
  function get_name() {
    return $this->first_name . " " . $this->surname;
  }
}
创建从
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
)派生模型,这将返回所有属性(自定义或其他属性)。

以下是我的简单解决方案。我已重写attributes方法并添加了以“get\u attribute\u”开头的任何方法,以便在序列化过程中使用它们:

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(或我想要的任何其他内容),并将其显示在序列化的值中。

实际上,它很容易实现如下@add This to:

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

SomeModel.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 "";
    }
  }
将此添加到:


从数据库中可以找到该模型,
$user->first_name
$user->names
对应于表中的字段。请参见我答案中的编辑。attributes()似乎无法获取getter。我真的很想遍历所有属性。
// check for attribute as getter
if ( method_exists( $this, "get" ) && $this->get( $name ) ){ 
  $var = $this->get( $name );
  return $var;
}