dev-master
9999999-devYii2 classes
MIT
The Requires
by amnah
yii2 yii fixture softdelete soft-delete
Yii2 classes
A various collection of classes. Install using composer
"amnah/yii2-classes": "dev-master"
., (*1)
This class adds soft-delete functionality to ActiveRecord models., (*2)
Before you use this, please see this response to decide if you really need this functionality., (*3)
`delete_time`
(int or timestamp DEFAULT NULL) to your database tablepublic function behaviors() { return [ 'softDelete' => [ 'class' => 'amnah\yii2\behaviors\SoftDelete', // these are the default values, which you can omit 'attribute' => 'delete_time', 'value' => null, // this is the same format as in TimestampBehavior 'safeMode' => true, // this processes '$model->delete()' calls as soft-deletes ], ]; }
// soft-delete model $model->remove(); // restore model $model->restore(); // delete model from db $model->forceDelete(); // soft-delete model if $safeMode = true // delete model from db if $safeMode = false $model->delete();
class Customer extends ActiveRecord { public static function find() { return parent::find()->where(['delete_time' => null]); } }
The ExtListView class extends the default yii\widgets\ListView
class by adding in
views and closures., (*4)
Using views:, (*5)
// @app/views/list/index.php <?php echo ExtListView::widget([ // ... "layoutView" => "_list", "layoutViewParams" => [ // variables to pass into layoutView ], "emptyView" => "_listEmpty", "emptyViewParams" => [ // variables to pass into emptyView ], // ... ]); ?>
// @app/views/list/_list.php{summary}{pager}{items}
Using closures:, (*6)
<?php echo ExtListView::widget([ // ... "layoutView" => function() { return ' <div>{summary}</div> <div>{pager}</div> <div id="listitems" class="row"> {items} </div> '; }, 'emptyView' => function() { return '<div>nothing found</div>'; }, // ... ]); ?>
The DbToDbFixtureManager class loads fixtures in from another database instead of from php arrays., (*7)
This is useful if you constantly change your db schema. For example, you can simply copy your developmentDb via phpMyAdmin and use that as your fixtureDb - no need to go through several php files and manually update data. From there, it's also easier to manipulate the data when you have sql commands at your disposal., (*8)
Additionally, this is useful if you have lots of fixture data. It is significantly faster to copy tables in sql than it is to load arrays in php and manually insert each individual record (which is the current implementation of DbFixtureManager)., (*9)
Note: Currently, the fixtureDb must be on the same db connection as the testDb (same server,
user, and password). This is because loads the data by using
insert into `fixtureDb`.`table select * from testDb.table
., (*10)
// @app/tests/unit/_config.php 'components' => [ 'fixture' => [ 'class' => 'amnah\yii2\test\DbToDbFixtureManager', 'fixtureDb' => 'databasename_test', ], ]
// @app/tests/unit/models/UserTest.php protected function setUp() { parent::setUp(); // load fixtures using same exact call $this->loadFixtures(['tbl_user']); }
Yii2 classes
MIT
yii2 yii fixture softdelete soft-delete