TP框架的自动绑定
对于某些操作的情况(例如模型的写入和更新方法),可以支持参数的自动绑定,例如:
首先需要开启DB_BIND_PARAM配置参数:
‘DB_BIND_PARAM‘ => true
然后,我们在使用
1.$Model = M(‘User‘); 2.$Model->name = ‘thinkphp‘; 3.$Model->email = ‘[email protected]‘; 4.$Model->add();
会自动对写入的数据进行参数绑定操作。其操作等效于:
1.$Model = M(‘User‘); 2.$Model->name = ‘:name‘; 3.$Model->email = ‘:email‘; 4.$bind[‘:name‘] = ‘thinkphp‘; 5.$bind[‘:email‘] = ‘[email protected]‘; 6.$Model->bind($bind)->add();
当然参数可以为变量:
1.$Model->username = $username; 2.$Model->password = $password; 3.$Model->email = $email; 4.$Model->phone = $phone; 5.$re=$Model->add();
时间: 2024-12-23 21:00:23