一蹲堂、模型和關(guān)聯(lián)
(一)基礎(chǔ)操作
- 1狼讨、新增數(shù)據(jù)
兩種方式新增用戶
<?php
namespace app\index\controller;
use app\index\model\User;
class UserController{
// 新增用戶數(shù)據(jù)
public function add(){
$user = new User;
$user->nickname = '流年';
$user->email = 'thinkphp@qq.com';
$user->birthday = strtotime('1977-03-05');
$user->save()
}
}
// 新增用戶數(shù)據(jù)
public function add(){
$user['nickname'] = '看云';
$user['email'] = 'kancloud@qq.com';
$user['birthday'] = strtotime('2015-04-02');
$result = UserModel::create($user)
}
create方法可以傳入數(shù)組或者標(biāo)準(zhǔn)對象,可以在外部統(tǒng)一賦值后傳入柒竞,當(dāng)然也可以直接傳入表單數(shù)據(jù)政供。
- 2、批量新增
// 批量新增用戶數(shù)據(jù)
public function addList(){
$user = new UserModel;
$list = [
['nickname' => '張三', 'email' => 'zhanghsan@qq.com', 'birthday' => strtotime('1988-01-15')],
['nickname' => '李四', 'email' => 'lisi@qq.com', 'birthday' => strtotime('1990-09-19')],
];
if ($user->saveAll($list)) {
return '用戶批量新增成功';
} else {
return $user->getError();
}
}
- 3朽基、查詢數(shù)據(jù)
// 讀取用戶數(shù)據(jù)
public function read($id='')
{
$user = UserModel::get($id);
echo $user->nickname . '<br/>';
echo $user->email . '<br/>';
echo date('Y/m/d', $user->birthday) . '<br/>';
}
模型的get方法和Db類的find方法返回結(jié)果的區(qū)別在于布隔,Db類默認(rèn)返回的只是數(shù)組(注意這里說的默認(rèn),其實仍然可以設(shè)置為對象)稼虎,而模型的get方法查詢返回的一定是當(dāng)前的模型對象實例衅檀。
- 4、數(shù)據(jù)列表
查詢多個數(shù)據(jù)時霎俩,可以使用all方法:
// 獲取用戶數(shù)據(jù)列表
public function index()
{
$list = UserModel::all();
foreach ($list as $user) {
echo $user->nickname . '<br/>';
echo $user->email . '<br/>';
echo date('Y/m/d', $user->birthday) . '<br/>';
echo '----------------------------------<br/>';
}
}
如果不是使用主鍵查詢术吝,可以直接傳入數(shù)組條件查詢
// 獲取用戶數(shù)據(jù)列表
public function index()
{
$list = UserModel::all(['status'=>1]);
foreach ($list as $user) {
echo $user->nickname . '<br/>';
echo $user->email . '<br/>';
echo date('Y/m/d', $user->birthday) . '<br/>';
echo '----------------------------------<br/>';
}
}
也可以使用閉包并配合查詢構(gòu)建器完成更多的條件查詢
// 獲取用戶數(shù)據(jù)列表
public function index()
{
$list = UserModel::all(function($query){
$query->where('id', '<', 3)->order('id', 'desc');
});
foreach ($list as $user) {
echo $user->nickname . '<br/>';
echo $user->email . '<br/>';
echo date('Y/m/d', $user->birthday) . '<br/>';
echo '----------------------------------<br/>';
}
}
- 5、刪除數(shù)據(jù)
// 刪除用戶數(shù)據(jù)
public function delete($id)
{
$user = UserModel::get($id);
if ($user) {
$user->delete();
return '刪除用戶成功';
} else {
return '刪除的用戶不存在';
}
}
同樣我們也可以直接使用destroy方法刪除模型數(shù)據(jù)
// 刪除用戶數(shù)據(jù)
public function delete($id)
{
$result = UserModel::destroy($id);
if ($result) {
return '刪除用戶成功';
} else {
return '刪除的用戶不存在';
}
}
(二)讀取器和修改器
- 1茸苇、讀取器:在讀取數(shù)據(jù)時為讀取到的數(shù)據(jù)設(shè)置一定的輸出格式排苍;
讀取器的命名規(guī)范:get + 屬性名的駝峰命名+ Attr
<?php
namespace app\index\model;
use think\Model;
class User extends Model{
// birthday讀取器
protected function getBirthdayAttr($birthday){
return date('Y-m-d', $birthday);
}
}
讀取器定義完后,修改控制器中方法:
// 讀取用戶數(shù)據(jù)
public function read($id='')
{
$user = UserModel::get($id);
echo $user->nickname . '<br/>';
echo $user->email . '<br/>';
echo $user->birthday . '<br/>';
}
- 2学密、修改器
命名規(guī)則:set + 屬性名的駝峰命名+ Attr
上述birthday是整型格式的淘衙,每次進(jìn)行復(fù)制時都要進(jìn)行轉(zhuǎn)換,因此用修改器解決以上問題
<?php
namespace app\index\model;
use think\Model;
class User extends Model{
// 讀取器
protected function getUserBirthdayAttr($birthday, $data){
return date('Y-m-d', $data['birthday']);
}
// birthday修改器
protected function setBirthdayAttr($value){
return strtotime($value);
}
}
(三)類型轉(zhuǎn)換和自動生成
通過類型轉(zhuǎn)換代替讀取器和修改器的功能
<?php
namespace app\index\model;
use think\Model;
class User extends Model{
protected $dateFormat = 'Y/m/d';
protected $type = [
// 設(shè)置birthday為時間戳類型(整型)
'birthday' => 'timestamp',
];
}