think\Model::setAttr PHP 메소드

setAttr() 공개 메소드

修改器 设置数据对象值
public setAttr ( string $name, mixed $value, array $data = [] )
$name string 属性名
$value mixed 属性值
$data array 数据
    public function setAttr($name, $value, $data = [])
    {
        if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) {
            // 自动写入的时间戳字段
            $value = $this->autoWriteTimestamp($name);
        } else {
            // 检测修改器
            $method = 'set' . Loader::parseName($name, 1) . 'Attr';
            if (method_exists($this, $method)) {
                $value = $this->{$method}($value, array_merge($data, $this->data));
            } elseif (isset($this->type[$name])) {
                // 类型转换
                $value = $this->writeTransform($value, $this->type[$name]);
            }
        }
        // 标记字段更改
        if (!isset($this->data[$name]) || $this->data[$name] != $value && !in_array($name, $this->change)) {
            $this->change[] = $name;
        }
        // 设置数据对象属性
        $this->data[$name] = $value;
        return $this;
    }

Usage Example

예제 #1
0
파일: Relation.php 프로젝트: GDdark/cici
 /**
  * 一对一 关联模型预查询拼装
  * @access public
  * @param string    $model 模型名称
  * @param string    $relation 关联名
  * @param Model     $result 模型对象实例
  * @return void
  */
 protected function match($model, $relation, &$result)
 {
     // 重新组装模型数据
     foreach ($result->toArray() as $key => $val) {
         if (strpos($key, '__')) {
             list($name, $attr) = explode('__', $key, 2);
             if ($name == $relation) {
                 $list[$name][$attr] = $val;
                 unset($result->{$key});
             }
         }
     }
     if (!isset($list[$relation])) {
         // 设置关联模型属性
         $list[$relation] = [];
     }
     $result->setAttr($relation, (new $model($list[$relation]))->isUpdate(true));
 }