生成model
php bin/hyperf.php gen:model user
User model app/Model/User.php
<?php declare (strict_types=1); namespace App\Model; use Hyperf\DbConnection\Model\Model; /** */ class User extends Model { /** * The table associated with the model. * * @var string */ protected $table = 'user'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = []; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = []; /** * 不自动维护时间戳 * @var bool */ public $timestamps = false; }
模型成员变量
参数 类型 默认值 备注 connection string default 数据库连接 table string 无 数据表名称 primaryKey string id 模型主键 keyType string int 主键类型 fillable array [] 允许被批量赋值的属性 casts string 无 数据格式化配置 timestamps bool true 是否自动维护时间戳 incrementing bool true 是否自增主键
模型查询
use App\Model\User; $user = User::query()->where('id', 1)->first(); $user = User::query()->where('id', 1)->find(1); $freshUser = $user->fresh(); $count = User::query()->where('age', 23)->count();
插入
use App\Model\User; $user = new User(); $user->name = 'xiaohong'; $user->age = 23; $bool = $user->save();
更新
$user = User::query()->where('id', 1)->find(1); $user->name = 'xiaohong'; $user->age = 23; $bool = $user->save();
批量更新
User::query()->where('age', 23)->update(['age' => '24']);
删除
User::query()->where('id', 3)->delete();