leinonen/yii2-eloquent

This package is abandoned and no longer maintained. No replacement package was suggested.

Drop in implementation of the Illuminate Database component for Yii2

Installs: 1 678

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 3

Open Issues: 1

Type:yii2-extension

0.5.6 2015-12-09 19:55 UTC

This package is not auto-updated.

Last update: 2020-01-20 21:27:24 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Build Status SensioLabsInsight

Yii2-eloquent

A drop in Laravels Eloquent and Illuminate/Database implementation for Yii2

Features:

  • Working extension (Still need to check Eloquent events and pagination)
  • Migrations
  • Fixtures
  • Yii style model validation for Eloquent models
  • 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.

composer require leinonen/yii2-eloquent

Configuration:

To configure the package just override and bootstrap your Yii db component in the application config.

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:

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.

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

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.

Simple example of Eloquent Model:

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;

Migrations

If you want to use Yiis basic migration commands you have overwrite the default migration controller in the console app config.

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:

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:

use leinonen\Yii2Eloquent\Fixtures\EloquentFixture;

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

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