lingyun / think-repositories
Thinkphp 数据库操作库
v1.0.2
2023-09-24 16:49 UTC
Requires
- topthink/framework: ^6.0||^8.0
This package is auto-updated.
Last update: 2024-09-24 19:06:38 UTC
README
think-repositories 是一个为 Thinkphp 6.0 设计的包,用于抽象数据库层。这使得应用程序更容易维护。
安装
在您的终端中运行以下命令
composer require think/repositories
使用方法
首先,创建您的仓库类。请注意,您的仓库类必须扩展 think\Repository
并实现 model() 方法
<?php namespace app\repositories; use think\Repository; class FilmsRepository extends Repository { public function model() { return 'App\Film'; } }
可以使用 make:R
命令创建
php think make:R admin@data/City common@data/DataCity --E=common@BaseRepository
通过实现 model()
方法,您告诉仓库您希望使用哪个模型类。现在,创建 App\Film
模型
<?php namespace app\model; use think\Model; class Film extends Model { protected $primaryKey = 'film_id'; protected $table = 'film'; protected $casts = [ "rental_rate" => 'float' ]; }
最后,在控制器中使用仓库
<?php namespace app\controller; use app\repositories\FilmsRepository as Film; class FilmsController extends Controller { private $film; public function __construct(Film $film) { $this->film = $film; } public function index() { return \Response::json($this->film->all()); } }
感谢
这个包在很大程度上受到了 这个优秀包 的启发,作者为 @bosnadev。