2017 © Pedro Peláez
 

yii2-extension yii2-eloquent

Drop in implementation of the Illuminate Database component for Yii2

image

leinonen/yii2-eloquent

Drop in implementation of the Illuminate Database component for Yii2

  • Thursday, January 14, 2016
  • by lordthorzonus
  • Repository
  • 1 Watchers
  • 1 Stars
  • 52 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 4 Versions
  • 4 % Grown

The README.md

Latest Stable Version Total Downloads Latest Unstable Version License Build Status SensioLabsInsight, (*1)

Yii2-eloquent

A drop in Laravels Eloquent and Illuminate/Database implementation for Yii2, (*2)

Features:

  • [x] Working extension (Still need to check Eloquent events and pagination)
  • [x] Migrations
  • [x] Fixtures
  • [x] Yii style model validation for Eloquent models
  • [x] Ability to feed Eloquent models to ActiveForm widget
  • [ ] Model factories for use with testing instead of Fixtures?
  • [ ] Other Laravel test helpers for Eloquent models?
  • [ ] Adapter for Yii::$app->db? It's confusing to use it now in IDE with autocompletion and creating an own Yii base class is too much work
  • [ ] Better Docs

Installation

Require this package, with Composer, in the root directory of your project., (*3)

composer require leinonen/yii2-eloquent

Configuration:

To configure the package just override and bootstrap your Yii db component in the application config., (*4)

use leinonen\Yii2Eloquent\Yii2Eloquent;
...
'bootstrap' => ['db'],
'components' => [
    'db' => [
        'class' => Yii2Eloquent::class,
        'driver' => 'mysql',
        'database' => 'yii2basic',
        'prefix' => '',
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'secret',
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
    ];

Usage:

Like said, by default the package overrides Yii's database component. So one you can access Illuminate\Database\Capsule\Manager like this:, (*5)

use Illuminate\Database\Schema\Blueprint;

Yii::$app->db->schema()->create('user', function (Blueprint $table) {
    $table->increments('id');
    $table->string('email')->unique();
    $table->timestamps();
});

However its much more preferable to access the capsule with static methods or inject it via dependency injection. IDEs cannot autocomplete accessing the db component from the Yii god object., (*6)

use Illuminate\Database\Capsule\Manager as Capsule;

$users = Capsule::table('users')->where('votes', '>', 100)->get();

$results = Capsule::select('select * from users where id = ?', array(1));

For complete docs please refer to: Illuminate\Database and Laravel, (*7)

Eloquent

Using Eloquent is covered in Laravels documentation. However this package provides also an altenrative base model which to extend form. By extending from leinonen\Yii2Eloquent\Eloquent\Model instead of Illuminate\Database\Eloquent\Model you'll get some Yii functionalities to your Eloquent models. For example you can then declare rules() scenarios() etc. as you are used with Yiis AR models. These can then be also fed to ActiveForm widget, validated using $model->validate() etc., (*8)

Simple example of Eloquent Model:, (*9)

use leinonen\Yii2Eloquent\Eloquent\Model;

class Order extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'order';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ['address', 'required'],
            ['address', 'string', 'min' => 3],
        ];
    }
}

The package also provides a simple trait to make your Eloquent user model compatible with Yii's IdentityInterface. Just use leinonen\Yii2Eloquent\Eloquent\AuthenticatableTrait;, (*10)

Migrations

If you want to use Yiis basic migration commands you have overwrite the default migration controller in the console app config., (*11)

use leinonen\Yii2Eloquent\Migrations\MigrateController;

'controllerMap' => [
      'migrate' => [
            'class' => MigrateController::class,
      ],
],

Then all the commands php yii migrate/create etc. will work as expected. Note that you have to use the Capsulein migrations instead of refering to Yiis base migration class:, (*12)

use Illuminate\Database\Capsule\Manager as Capsule;
use yii\db\MigrationInterface;

class myMigration implements MigrationInterface
{
    public function up()
    {
        Capsule::schema()->create('my_table', function($table){
            $table->increments('id');
        });
    }

    public function down()
    {
        Capsule::schema()->dropIfExists('my_table');
    }
}

Fixtures

If you like Yiis fixtures no worries! Just extend leinonen\Yii2Eloquent\Fixtures\EloquentFixture when creating the Fixture class and everything will work as usual:, (*13)

use leinonen\Yii2Eloquent\Fixtures\EloquentFixture;

class OrderFixture extends EloquentFixture
{
    public $modelClass = Order::class;

    public function getData()
    {
        return [
            'example1' => [
                'address' => 'Test address',
            ],
        ];
    }
}

The Versions

14/01 2016

dev-master

9999999-dev

Drop in implementation of the Illuminate Database component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Juuso Leinonen

09/12 2015

0.5.6

0.5.6.0

Drop in implementation of the Illuminate Database component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Juuso Leinonen

09/12 2015

0.5.5

0.5.5.0

Drop in implementation of the Illuminate Database component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Juuso Leinonen

08/12 2015

0.5.0

0.5.0.0

Drop in implementation of the Illuminate Database component for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Juuso Leinonen