定制 jwh315/laravel-capsule-wrapper 二次开发

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

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

jwh315/laravel-capsule-wrapper

最新稳定版本:0.0.1

Composer 安装命令:

composer require jwh315/laravel-capsule-wrapper

包简介

Use the Eloquent ORM outside of the Laravel Framework with an existing PDO database connection.

README 文档

README

Use the Eloquent ORM outside of the Laravel Framework with an existing PDO database connection.

disclaimer

This project is currently a proof of concept, without tests. I have manually tested it using a few standard Laravel operations with success, but I cannot currently vouch for its robustness in a real world PHP application.

One of the main issues I have not fully fleshed out is the database name parameter that you would normally pass into an eloquent connection. I have chosen to omit this parameter as my use case requires a database connection, with the ability to access multiple databases on the same connection. From my limited testing, this hasn't posed a problem, but I could be missing something.

Lastly, this only works for Mysql currently. This is done in CapsuleManager\Wrapper\DatabaseManager::addDefaultConnection(), theoretically you should be able to swap out any of the other Illuminate\Database connection classes as needed.

installation

composer require jwh315/laravel-capsule-wrapper

usage

This is pretty much the same as the usage instructions on the Illuminate\Database repository. It should be noted that I have not tested the Schema Builder

use CapsuleManager\Wrapper\Manager;
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;

$dsn = "mysql:host=" . DBHOST;
$pdo = new \PDO($dsn, DBUSER, DBPASS);

$capsule = new Manager($pdo);

// Set the event dispatcher used by Eloquent models... (optional)
$capsule->setEventDispatcher(new Dispatcher(new Container));

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();


$t = $capsule::table('mysql.db')
			 ->where('Host', 'localhost')
			 ->get();

dd($t);

I also added a static accessor to encapsulate all of this boilerplate inside of the wrapper class, if you are going to use the defaults settings in the above example, this will also work.

use CapsuleManager\Wrapper\Manager as Capsule;

$dsn = "mysql:host=" . DBHOST;
$pdo = new \PDO($dsn, DBUSER, DBPASS);

$capsule = Capsule::init($pdo);

$t = $capsule::table('mysql.db')
			 ->where('Host', 'localhost')
			 ->get();

dd($t);

Using the Eloquent ORM

This is exactly the same as you would expect, except as mentioned above, I did not add a way to set the database name so you will have to set it explicitly

//initialize the capsule manager
use CapsuleManager\Wrapper\Manager as Capsule;

$dsn = "mysql:host=" . DBHOST;
$pdo = new \PDO($dsn, DBUSER, DBPASS);

$capsule = Capsule::init($pdo);

//user model example
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
	//this needs to be set on all your models
	protected $table = 'test.users';
}

//using the user model
dd(User::all()->last()->login);

统计信息

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

GitHub 信息

  • Stars: 7
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-19