skytells/database

Skytells 数据库包。

1.1 2017-12-30 21:42 UTC

This package is not auto-updated.

Last update: 2024-09-18 04:54:12 UTC


README

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

使用说明

首先,创建一个新的“Capsule”管理实例。Capsule旨在使配置库以便在Laravel框架之外使用尽可能简单。

use Skytells\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 Skytells\Events\Dispatcher;
use Skytells\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 "skytells/events" 当您需要使用Eloquent的观察者时需要。

Capsule实例注册后,您可以使用它如下

使用查询构建器

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

其他核心方法可以直接从Capsule访问,就像从DB外观一样

$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 Skytells\Database\Eloquent\Model {}

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

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