tp模型和数据库操作方法
生活随笔
收集整理的這篇文章主要介紹了
tp模型和数据库操作方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、新建的模型名和表名一樣,采用駝峰式,如表名user_type模型取名為UserType
namespace app\index\model;use think\Model;
class UserType extends Model
{
}
--------------
控制器調用方法如下 use app\index\model\User; //插入方法一 /*
$user=new User;
$user->username='hehe';
$user->password='123456';
$user->status='1';
$user->save();
*/
//插入方法二
/*
$data['username']='test';
$data['password']='pass';
User::create( $data);
*/
//批量插入方法一
/*
$user=new User;
$list=[
['username'=>'aaa','password'=>'123'],
['username'=>'bbb','password'=>'456']
];//二維數(shù)組
$user->saveAll($list);
*/
//以ID的方式查詢數(shù)據(jù)
/*
$user=User::get(3);//查詢ID為3的單條數(shù)據(jù)
echo $user->username;//以對象的方式顯示對應的字段值
echo $user['password'];//以數(shù)組的方式顯示
*/
//按指定的字段查詢User是模型類名 getByUsername中的Username是字段名 dome是值 查詢user表中username='dome' 顯示ID
/*
$user=User::getByUsername('dome');
echo $user->id;
*/
//多條件查詢
/*
echo $user=User::get(['username'=>'dome','password'=>'pass5']);//查詢關系是and
echo $user=User::where(['username'=>'dome','password'=>'pass5'])->find();//查詢關系是and
*/
//查詢所有數(shù)據(jù)
/*
$list=User::all();
$list=User::all(['id'=>5]);
*/
//更新數(shù)據(jù)方法一
/*
$user=User::get(5);//更新ID等于5
$user->username='u5';
$user->password='p5';
$user->save();
*/
//更新數(shù)據(jù)方法二
/*
$arr['username']='aaa';
$arr['password']='bbb';
User::update($arr,['id'=>5]);
*/
//刪除數(shù)據(jù)兩種
// User::get(5)->delete();//刪除ID等于5
//User::destroy(6);//刪除ID等于6 ?
轉載于:https://www.cnblogs.com/bk7788/p/7256837.html
總結
以上是生活随笔為你收集整理的tp模型和数据库操作方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]php-fpm配置具体解释
- 下一篇: 《ASP.NET MVC企业实战》(一)