Home >Backend Development >PHP Tutorial >After the create method in thinkphp automatically creates form data, will the fields in the form be saved as attributes of the model object?

After the create method in thinkphp automatically creates form data, will the fields in the form be saved as attributes of the model object?

WBOY
WBOYOriginal
2016-09-08 08:43:581006browse

Code in controller:

<code>if(IS_POST){
            if($model->validate($model->_login_validate)->create()){
                
                if($model->login()){
                    if($res = session('returnUrl')){
                        session('returnUrl',null);
                        $returnUrl = $res;
                    }else{
                        $returnUrl = U('/');
                    }
                    
                    $this->success('登陆成功!',$returnUrl);
                    die;
                }
            }
            $this->error($model->getError());
        }
</code>

Code in model:

<code>public function login(){
        var_dump($this);die;    //这里打印$this并没有找到username和password这两个属性,可是为什么下面的$username可以取到值呢??
        $username = $this->username;
        $password = $this->password;
        $model = M('Member');

        $where['username'] = $username;
        $user = $model->where($where)->find();
        。。。
}
</code>

Reply content:

Code in controller:

<code>if(IS_POST){
            if($model->validate($model->_login_validate)->create()){
                
                if($model->login()){
                    if($res = session('returnUrl')){
                        session('returnUrl',null);
                        $returnUrl = $res;
                    }else{
                        $returnUrl = U('/');
                    }
                    
                    $this->success('登陆成功!',$returnUrl);
                    die;
                }
            }
            $this->error($model->getError());
        }
</code>

Code in model:

<code>public function login(){
        var_dump($this);die;    //这里打印$this并没有找到username和password这两个属性,可是为什么下面的$username可以取到值呢??
        $username = $this->username;
        $password = $this->password;
        $model = M('Member');

        $where['username'] = $username;
        $user = $model->where($where)->find();
        。。。
}
</code>

It exists in the data. Use get to read the non-existing attributes and then go to the data. Similarly, you can set the attributes by set, that is, add the attributes directly after the object

<code> public function __get($name) {
        return isset($this->data[$name])?$this->data[$name]:null;
    }
    
    public function __set($name,$value) {
        // 设置数据对象属性
        $this->data[$name]  =   $value;
    }</code>

Yes, although I have never written it like this. But I printed the object after create and found that it has these attributes

<code>$data['name'] = 'ThinkPHP';
$data['email'] = 'ThinkPHP@gmail.com';
$User->create($data);
// 创建完成数据对象后可以直接读取数据
echo $User->name;
echo $User->email;
// 也可以直接修改创建完成的数据
$User->name = 'onethink'; // 修改name字段数据
$User->status = 1; // 增加新的字段数据</code>

http://www.kancloud.cn/manual...
I found this paragraph from the 3.2.3 document. After you create(), the $this->name; you accessed should be the attribute after creation. Try var_dump($model);. .

After the create method in thinkphp automatically creates form data, will the fields in the form be saved as attributes of the model object?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn