命令行生成User模型
php think make:model index/User
模型调用
$user = new \app\index\model\User(); $user = $user->where('id',3)->find(); var_dump($user->toArray());
$user = model('user')->where('id',3)->find(); var_dump($user->toArray());
添加一条数据
$user = new \app\index\model\User(); $user->name = 'huyongjian11'; $user->status = 1; $user->save();
添加多条数据
$user = model('user'); $list = [ ['name'=>'胡勇健1','status'=>1], ['name'=>'胡勇健2','status'=>0] ]; $user->saveAll($list);
$user = model('user')->where('id',5)->find(); $user->name = '胡勇健'; $user->save();
$user = model('user')->where('id',5)->find(); $user->delete();
$user = model('user')->get(6);
$user = model('user')->where('id',5)->find();
$users=model('user')->where('id','>',6)->select(); foreach ($users as $user){ dump($user->toArray()); }
model('user')->count(); model('user')->max('id'); model('user')->min('id'); model('user')->avg('id'); model('user')->sum('id');
User模型
<?php namespace app\index\model; use think\Model; class User extends Model { public function getStatusAttr($value) { $status = [-1=>'删除',0=>'禁用',1=>'正常',2=>'待审核']; return $status[$value]; } }
控制器
$user = model('user')->where('id',6)->find(); var_dump($user->status);
User模型
public function setNameAttr($value) { return strtolower($value); }
控制器
$user = model('user')->where('id',6)->find(); $user->name = 'HUYONGJIAN'; $user->save();
User模型
protected $autoWriteTimestamp = true;
protected $autoWriteTimestamp = 'datetime';
// 定义时间戳字段名 protected $createTime = 'create_at'; protected $updateTime = 'update_at';
三个模型层的定义如下:
app\index\model\User.php
<?php namespace app\index\model; use think\Model; class User extends Model { }
实例化方法:\think\Loader::model('User')
Logic类:app\index\logic\User.php
<?php namespace app\index\logic; use think\Model; class User extends Model { }
实例化方法:\think\Loader::model('User','logic');
Service类:app\index\service\User.php
<?php namespace app\index\service; use think\Model; class User extends Model { }
实例化方法:\think\Loader::model('User','service');
$user->toArray();
$user->toJson();
User模型
<?php namespace app\index\model; use think\Model; class User extends Model { //初始化 protected function initialize() { parent::initialize(); self::event('before_update', function ($user) { //事件处理 }); } }
标签位 描述 before_insert 新增前 after_insert 新增后 before_update 更新前 after_update 更新后 before_write 写入前 after_write 写入后 before_delete 删除前 after_delete 删除后
快捷注册
User::beforeInsert(function ($user) { if ($user->status != 1) { return false; } });
标签位 描述 beforeInsert 新增前 afterInsert 新增后 beforeUpdate 更新前 afterUpdate 更新后 beforeWrite 写入前 afterWrite 写入后 beforeDelete 删除前 afterDelete 删除后
一对一关联
hasOne('关联模型名','外键名','主键名',['模型别名定义'],'join类型');
用法
<?php namespace app\index\model; use think\Model; class User extends Model { public function profile() { //return $this->>hasOne('Profile'); return $this->hasOne('Profile','user_id','id'); } }
一对多关联
hasMany('关联模型名','外键名','主键名',['模型别名定义']);
用法
<?php namespace app\index\model; use think\Model; class User extends Model { public function comments(){ //return $this->hasMany('comment'); return $this->hasMany('comment','user_id','id'); } }