【问题标题】:PHP ActiveRecord doesn't seem to use my custom attributePHP ActiveRecord 似乎没有使用我的自定义属性
【发布时间】:2012-06-20 08:53:22
【问题描述】:

我有一个模型定义如下:

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

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

谢谢, 加雷斯

【问题讨论】:

    标签: php activerecord phpactiverecord


    【解决方案1】:

    这是我的简单解决方案。我已经覆盖了属性方法并添加了任何以“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(或任何我想要的)并让它显示在序列化值中。

    【讨论】:

      【解决方案2】:

      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 模型会使用它

      【讨论】:

      • 我真的很想遍历所有属性。
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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


        编辑

        我不确定 attributes() 方法是否会选择自定义 getter。好像只是返回了模型的attributes属性。

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

        【讨论】:

        • 从数据库中找到那个模型,$user->first_name$user->surname对应表上的字段。
        • 查看我的答案的编辑。 attributes() 似乎没有选择吸气剂。
        【解决方案5】:

        我解决了这个问题:

        创建一个派生自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)派生模型,这将返回所有属性,无论是自定义的还是其他的。

        【讨论】:

          【解决方案6】:

          其实这样很容易实现@add this to 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;
          }
          

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

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

          将此添加到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;
          } 
          

          【讨论】:

            猜你喜欢
            • 2010-11-13
            • 1970-01-01
            • 2015-07-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-01
            • 2017-12-30
            • 1970-01-01
            相关资源
            最近更新 更多