awsm3 / yii2-repository
Yii2 的仓库实现
1.0.3
2017-05-09 14:26 UTC
Requires
- yiisoft/yii2: *
Suggests
- yiisoft/yii2-mongodb: Needed if you want to use MongoDB as your dbms instead of relational dbms
This package is not auto-updated.
Last update: 2024-09-29 02:52:06 UTC
README
Yii2 仓库模式实现
您可以在此处了解更多关于仓库模式的内容
- http://deviq.com/repository-pattern/
- https://martinfowler.com.cn/eaaCatalog/repository.html
- http://shawnmc.cool/the-repository-pattern
- http://stackoverflow.com/questions/16176990/proper-repository-pattern-design-in-php
目录
安装
Composer
安装此扩展的首选方法是使用 composer。
运行以下命令之一
composer require --prefer-dist awsm3/yii2-repository "1.*"
或
"awsm3/yii2-repository": "1.*"
将其添加到您的 composer.json
文件的 require 部分。
方法
Awsm3\Yii2Repository\Interfaces\RepositoryInterface
public function with(array $with = []); public function columns(array $columns = ['*']); public function limit($limit = 10); public function orderBy($orderBy, $sort = 'DESC'); public function makeModel(); public function create(array $data); public function createMany(array $data); public function createByScenario(array $data, string $scenario = 'default'); public function createWithoutValidation(array $data); public function findOneById($id, $returnArray = false); public function findOneBy($key, $value, $operation = '=', $returnArray = false); public function findManyBy($key, $value, $operation = '=', $withPagination = true, $returnArray = false); public function findManyByIds(array $ids, $withPagination = true, $returnArray = false); public function findAll($withPagination = true, $returnArray = false); public function findOneByCriteria(array $criteria = [], array $with = [], bool $returnArray = false); public function findManyByCriteria(array $criteria = [], $withPagination = true, $with = [], $returnArray = false); public function updateOneById($id, array $data = []); public function updateOneBy($key, $value, array $data = []); public function updateOneByCriteria(array $criteria, array $data = []); public function updateManyBy($key, $value, array $data = [], $operation = '='); public function updateManyByCriteria(array $criteria = [], array $data = []); public function updateManyByIds(array $ids, array $data = []); public function allExist(array $ids); public function deleteOneBy($key, $value, $operation = '='); public function deleteOneByCriteria(array $criteria = []); public function deleteManyBy($key, $value, $operation = '='); public function deleteManyByCriteria(array $criteria = []); public function searchByCriteria(); public function deleteManyByIds(array $ids); public function inc($id, $field, $count = 1); public function dec($id, $field, $count = 1);
使用方法
创建一个模型
创建您的 ActiveRecord 模型
namespace app\models; use yii\db\ActiveRecord; /** * Class City * @package app\models */ class Post extends ActiveRecord { /** * @return string */ public static function tableName() { return 'posts'; } /** * @return array */ public function rules() { return [ [['title'], 'required'], ]; } }
创建一个仓库接口
namespace app\repositories\interfaces; use Awsm3\Yii2Repository\Interfaces\RepositoryInterface; interface PostRepositoryInterface extends RepositoryInterface { }
创建一个仓库类
namespace app\repositories; use app\repositories\interfaces\PostRepositoryInterface; use Awsm3\Yii2Repository\AbstractSqlArRepository; class PostRepository extends AbstractSqlArRepository implements PostRepositoryInterface { protected $primaryKey = 'id'; protected $applicationKey = 'id'; }
或者
创建一个作为 Yii 组件的仓库
class PostRepository extends Component implements PostRepositoryInterface { use SqlArRepositoryTrait { init as repositoryInit; } protected $primaryKey = 'id'; protected $applicationKey = 'id'; public function init() { parent::init(); $this->repositoryInit(); } }
这种方法在您有一个已经扩展了其他类的类时很有用,不能像使用 traits 一样通过扩展 AbstractRepository 类来作为 yii 组件使用。
如果您的模型使用 SQL 作为其数据源,并且模型实际上扩展了 yii\db\ActiveRecord,则您的仓库应该扩展 Awsm3\Yii2Repository\AbstractSqlArRepository。
如果您的数据源是 MongoDB,并且模型实际上扩展了 yii\mongodb\ActiveRecord,则您的仓库应该扩展 Awsm3\Yii2Repository\AbstractMongoArRepository。
考虑您的应用程序可以使用 SQL 和文档数据库(Mongo)的强大功能
将仓库附加到容器
Yii::$container->set(\app\repositories\interfaces\iPostRepository::class, [ 'class' => \app\repositories\PostRepository::class ]); Yii::$container->set('postRepository', function($container, $params, $config){ return new \app\repositories\PostRepository(new \app\models\Post()); });
如果您正在编写模块,可以将上述代码放在您的模块启动文件中。您还可以不使用容器,并在需要的地方创建仓库对象,如下所示
$postRepository = new \app\repositories\PostRepository(new \app\models\Post());
仓库示例使用
我通过控制器示例展示的示例用法。
namespace app\controllers; use app\repositories\interfaces\PostRepositoryInterface; use Yii; use yii\web\Controller; use yii\web\Response; /** * PostController */ class PostController extends Controller { /** * @var iPostRepository */ protected $postRepository; /** * @var bool */ public $enableCsrfValidation = false; /** * init */ public function init() { parent::init(); $this->postRepository = Yii::$container->get('postRepository'); Yii::$app->response->format = Response::FORMAT_JSON; } /** * @return array */ public function verbs() { return [ 'create' => ['POST'], 'delete' => ['DELETE'], 'update' => ['PUT'], 'index' => ['GET'], 'show' => ['GET'], 'delete-multiple' => ['DELETE'] ]; } /** * @return mixed */ public function actionCreate() { $data = Yii::$app->request->post(); $post = $this->postRepository->create($data); return $post; } /** * @param $id */ public function actionDelete($id) { $this->postRepository->deleteOneById($id); } /** * @param $id * @return bool */ public function actionUpdate($id) { $data = Yii::$app->request->post(); $post = $this->postRepository->updateOneById($id, $data); return $post; } /** * @return mixed */ public function actionIndex() { return $this->postRepository->searchByCriteria(); } /** * @param $id * @return mixed */ public function actionShow($id) { return $this->postRepository->findOneById($id); } /** * */ public function actionDeleteMultiple() { $ids = Yii::$app->request->post()['ids']; $deletedCount = $this->postRepository->deleteManyByIds($ids); } }