Yii2 classes

Installs: 3 111

Dependents: 0

Suggesters: 0

Security: 0

Stars: 12

Watchers: 3

Forks: 3

Open Issues: 1


dev-master 2015-07-20 00:38 UTC

This package is not auto-updated.

Last update: 2024-05-11 13:00:02 UTC


A various collection of classes. Install using composer "amnah/yii2-classes": "dev-master".

Table of Contents

  • [Behaviors/SoftDelete] (#softdelete)
  • [Widgets/ExtListView] (#extlistview)
  • [Test/DbToDbFixtureManager] (#dbtodbfixturemanager)


This class adds soft-delete functionality to ActiveRecord models.

Before you use this, please see this response to decide if you really need this functionality.


  • Add column `delete_time` (int or timestamp DEFAULT NULL) to your database table
  • Add behavior to your model
public 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
  • Call functions
// soft-delete model

// restore model

// delete model from db

// soft-delete model if $safeMode = true
// delete model from db if $safeMode = false
  • (Optional) Update model by adding default condition/scope to select only non-deleted records
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.


Using views:

// @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

<div id="listitems" class="row">

Using closures:

<?php echo ExtListView::widget([
    // ...
    "layoutView" => function() {
        return '
            <div id="listitems" class="row">
    'emptyView' => function() {
        return '<div>nothing found</div>';
    // ...
]); ?>


The DbToDbFixtureManager class loads fixtures in from another database instead of from php arrays.

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.

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).

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.


// @app/tests/unit/_config.php
'components' => [
    'fixture' => [
        'class' => 'amnah\yii2\test\DbToDbFixtureManager',
        'fixtureDb' => 'databasename_test',
// @app/tests/unit/models/UserTest.php
protected function setUp() {

    // load fixtures using same exact call