rootlocal / yii2-crud
Create, read, update and delete extension
Installs: 560
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=7.4||^8.0||^8.1
- yiisoft/yii2: ^2.0
Requires (Dev)
- codeception/codeception: ^4.1
- codeception/module-asserts: ^2.0
- codeception/module-filesystem: ^1.0
- codeception/module-phpbrowser: ^1.0
- codeception/module-yii2: ^1.1
- codeception/verify: ^2.2
- pdepend/pdepend: ^2.10
- phploc/phploc: ^7.0
- phpmd/phpmd: ^2.12
- sebastian/phpcpd: ^6.0
- squizlabs/php_codesniffer: 3.*
- symfony/browser-kit: >=2.7 <=4.2.4
- yiisoft/yii2-bootstrap: ~2.0.0
- yiisoft/yii2-faker: ~2.0.0
README
Install
composer require rootlocal/yii2-crud
or add
"rootlocal/yii2-crud": "~1.0",
to the require section of your composer.json.
Standard Controller:
<?php namespace rootlocal\crud\test\app\controllers; use rootlocal\crud\test\app\models\db\Book; use rootlocal\crud\test\app\models\search\BookSearch; use rootlocal\crud\controllers\ActiveController; /** * Class CrudController * @package rootlocal\crud\test\app\controllers */ class CrudController extends ActiveController { /** @var string */ public $modelClass = Book::class; /** @var string */ public $modelSearchClass = BookSearch::class; }
OR
<?php use rootlocal\crud\test\app\models\db\Book; use rootlocal\crud\test\app\models\search\BookSearch; use yii\web\Controller; use rootlocal\crud\actions\CreateAction; use rootlocal\crud\actions\DeleteAction; use rootlocal\crud\actions\IndexAction; use rootlocal\crud\actions\UpdateAction; use rootlocal\crud\actions\ValidateAction; use rootlocal\crud\actions\ViewAction; /** * Class BookController * @package rootlocal\crud\test\app\controllers */ class BookController extends Controller { // ... /** * {@inheritdoc} * @return array */ public function actions() { return [ 'index' => [ 'class' => IndexAction::class, 'searchModel' => BookSearch::class, ], 'view' => [ 'class' => ViewAction::class, 'model' => Book::class, ], 'validate' => [ 'class' => ValidateAction::class, 'model' => Book::class, 'scenario' => Book::SCENARIO_CREATE, ], 'create' => [ 'class' => CreateAction::class, 'model' => Book::class, 'scenario' => Book::SCENARIO_CREATE, ], 'update' => [ 'class' => UpdateAction::class, 'model' => Book::class, 'scenario' => Book::SCENARIO_UPDATE, ], 'delete' => [ 'class' => DeleteAction::class, 'model' => Book::class, ], ]; } // ... }
Form:
<?php use rootlocal\crud\test\app\models\db\Book; use yii\helpers\Html; use yii\web\View; use yii\widgets\ActiveForm; use yii\helpers\Url; /** * @var $this View * @var $model Book * @var $form ActiveForm */ ?> <div class="book-form"> <?php $form = ActiveForm::begin([ 'id' => 'book-form', 'enableAjaxValidation' => true, 'validationUrl' => Url::to(['validate', 'id' => $model->isNewRecord ? null : $model->id, 'scenario' => $model->isNewRecord ? $model::SCENARIO_CREATE : $model::SCENARIO_UPDATE, ])]); ?> <?= $form->errorSummary($model) ?> <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?> <?= $form->field($model, 'status')->dropDownList($model->getStatusItems()) ?> <div class="form-group"> <?= Html::submitButton('Save', ['class' => 'btn btn-success btn-sm']) ?> </div> <?php ActiveForm::end(); ?> </div>
Search Model:
<?php use rootlocal\crud\test\app\models\db\Book; use yii\data\ActiveDataProvider; use rootlocal\crud\components\SearchModelInterface; use rootlocal\crud\test\app\models\query\BookQuery; /** * Class BookSearch * @package rootlocal\crud\test\app\models * * @property BookQuery $query */ class BookSearch extends Book implements SearchModelInterface { /** * @var BookQuery */ private $_query; // ... /** * @return BookQuery */ public function getQuery(): BookQuery { if ($this->_query === null) { $this->_query = self::find(); } return $this->_query; } /** * @param BookQuery $query */ public function setQuery(BookQuery $query): void { $this->_query = $query; } /** * Creates data provider instance with search query applied * @param array $params * @return ActiveDataProvider */ public function search($params = []) { $query = $this->getQuery(); $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); // ... return $dataProvider; } }
Extended Controller:
<?php use rootlocal\crud\test\app\models\db\Book; use rootlocal\crud\test\app\models\search\BookSearch; use yii\web\Controller; use rootlocal\crud\actions\CreateAction; use rootlocal\crud\actions\DeleteAction; use rootlocal\crud\actions\IndexAction; use rootlocal\crud\actions\UpdateAction; use rootlocal\crud\actions\ValidateAction; use rootlocal\crud\actions\ViewAction; /** * Class BookController * @package rootlocal\crud\test\app\controllers */ class BookController extends Controller { // ... /** * {@inheritdoc} * @return array */ public function actions() { return [ 'index' => [ 'class' => IndexAction::class, 'searchModel' => BookSearch::class, 'dataProvider' => function ($model, $queryParams) { /** * @var BookSearch $model * @var array $queryParams */ $model->query = $model::find()->active(); return $model->search($queryParams); } ], 'view' => [ 'class' => ViewAction::class, 'model' => function ($id) { /** * @var int $id */ return Book::find()->active()->where(['id' => $id])->one(); } ], // etc ... ]; } // ... }