PHP code example of imliam / yii2-traits

1. Go to this page and download the library: Download imliam/yii2-traits library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

imliam / yii2-traits example snippets




use yii\db\Migration;
use ImLiam\Yii2Traits\MigrationHelpers;

class m180524_091606_run_migration extends Migration
{
    use MigrationHelpers;

    public function safeUp()
        $this->addColumn('users', 'country', 'string');
        $this->fillColumn('users', 'country', 'GB');
    }
}



use yii\db\ActiveRecord;
use ImLiam\Yii2Traits\ActiveRecordHelpers;

class User extends ActiveRecord
{
    use ActiveRecordHelpers;
}

$user = User::firstOrCreate(['id' => 1], ['username' => 'Admin']);
// Returns user ID 1 if it exists in the database, or creates
// a new user with the ID of 1 and username of 'Admin'



use yii\db\ActiveRecord;
use ImLiam\Yii2Traits\ActiveRecordHelpers;

class User extends ActiveRecord
{
    use ActiveRecordHelpers;
}

$user = User::create(['username' => 'Admin']);
// Creates and returns a new user with the username of 'Admin'



use yii\db\ActiveRecord;
use ImLiam\Yii2Traits\ActiveRecordHelpers;

class User extends ActiveRecord
{
    use ActiveRecordHelpers;
}

$user = User::make(['username' => 'Admin']);
// Makes a new user with the username of 'Admin' but does NOT save it to the database



use yii\db\ActiveRecord;
use ImLiam\Yii2Traits\ActiveRecordHelpers;

class User extends ActiveRecord
{
    use ActiveRecordHelpers;
}

User::deleteIfExists(['username' => 'Admin']);
// Deletes all users with the username of 'Admin'



use yii\db\ActiveRecord;
use ImLiam\Yii2Traits\ActiveRecordHelpers;

class User extends ActiveRecord
{
    use ActiveRecordHelpers;
}

$user = User::first();
// Returns the first user in the database