定制 leinonen/yii2-eloquent 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

leinonen/yii2-eloquent

最新稳定版本:0.5.6

Composer 安装命令:

composer require leinonen/yii2-eloquent

包简介

Drop in implementation of the Illuminate Database component for Yii2

README 文档

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',
            ],
        ];
    }
}

统计信息

  • 总下载量: 1.77k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固