yoncode/database

此包已被废弃,不再维护。作者建议使用 yoncode/database 包。

Illuminate 数据库修改包以支持 Silex 应用程序和 PHP 5.3。

v5.2.7 2016-01-06 16:20 UTC

README

Illuminate 数据库组件是 PHP 的完整数据库工具包,提供表达式查询构建器、ActiveRecord 风格的 ORM 和架构构建器。它目前支持 MySQL、Postgres、SQL Server 和 SQLite。它还作为 Laravel PHP 框架的数据库层。

使用说明

首先,创建一个新的 "Capsule" 管理器实例。Capsule 的目的是使在 Laravel 框架之外配置库尽可能容易。

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => 'password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
]);

// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$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();

composer require "illuminate/events" 在您需要使用 Eloquent 的观察者时是必需的。

一旦注册了 Capsule 实例,您可以像这样使用它

使用查询构建器

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

可以通过与 DB 门面相同的方式直接从 Capsule 访问其他核心方法

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

使用架构构建器

Capsule::schema()->create('users', function($table)
{
    $table->increments('id');
    $table->string('email')->unique();
    $table->timestamps();
});

使用 Eloquent ORM

class User extends Illuminate\Database\Eloquent\Model {}

$users = User::where('votes', '>', 1)->get();

有关使用此库提供的各种数据库功能的进一步文档,请参阅 Laravel 框架文档