jguyomard/silex-capsule-eloquent

Silex 2 的 Capsule/Eloquent 服务提供者

v2.0.2 2016-10-21 07:34 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:25:30 UTC


README

Travis StyleCI Packagist Release Licence

这是一个为 Silex 2.0 提供服务的提供者,它通过 Capsule 集成了 Laravel 的 Fluent Query BuilderEloquent ORM

安装

注意:此服务提供者需要 silex/silex ~2.0

composer require jguyomard/silex-capsule-eloquent "~2.0"

用法

这是使用 MySQL 的基本配置(目前,Laravel 支持 MySQL、Postgres、SQLite 和 SQL Server)

$app = new Silex\Application();

$app->register(
    new \JG\Silex\Provider\CapsuleServiceProvider(),
    [
        'capsule.connections' => [
            'default' => [
                'driver'    => 'mysql',
                'host'      => 'localhost',
                'database'  => 'mydatabase',
                'username'  => 'root',
                'password'  => 'root',
            ]
        ]
    ]
);

这是基本用法,使用 查询构建器原始 SQL 查询

$app->get('/article/{id}', function(Application $app, $id)
{
    $article = Capsule::table('article')->where('id', $id)->get();

    // Rest of your code...
});

$app->get('/raw/{id}', function(Application $app, $id)
{
    $article = Capsule::select('SELECT * FROM article WHERE id = :id', [
        'id' => $id,
    ]);

    // Rest of your code...
});

$app->run();

您还可以使用 Eloquent 模型

class ArticleModel extends Model
{
    protected $table = 'article';

    protected $primaryKey = 'id';

    protected $fillable = [
        'title'
    ];

    // Rest of your code...
}

$app->get('/article/{id}', function(Application $app, $id)
{
    $article = ArticleModel::find($id);

    // Rest of your code...
});

$app->post('/article', function(Application $app)
{
    $article = ArticleModel::create([
        'title' => 'Foo'
    ]);

    // Rest of your code...
});

$app->run();

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

配置

这是一个完整的配置示例,包含多个连接

$app = new Silex\Application();

$app->register(
    new \JG\Silex\Provider\CapsuleServiceProvider(),
    [
        'capsule.connections' => [
            'default' => [
                'driver'    => 'mysql',
                'host'      => 'localhost',
                'port'      => 3306,
                'database'  => 'mydatabase',
                'username'  => 'root',
                'password'  => 'root',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
                'strict'    => false,
                'engine'    => null,
            ],
            'pgsql' => [
                'driver' => 'pgsql',
                'host'      => 'localhost',
                'port'      => 5432,
                'database'  => 'mydatabase',
                'username'  => 'root',
                'password'  => 'root',
                'charset'   => 'utf8',
                'prefix'    => '',
                'schema'    => 'public',
            ],
            'sqlite' => [
                'driver' => 'sqlite',
                'database'  => 'mydatabase',
                'prefix' => '',
            ],
        ],
        'capsule.options' => [
            'setAsGlobal'    => true,
            'bootEloquent'   => true,
            'enableQueryLog' => true,
        ],
    ]
);

测试

要运行测试套件,您需要 PHPUnit

phpunit

致谢

illuminate-database-silex-service-provider(用于 Silex 1.*)和 saxulum-doctrine-mongodb-odm-provider@dev(Mongodb ODM 用于 Silex 2.0.x-dev)的启发。

问题

如果您对此服务提供者有任何问题或疑问,请通过 GitHub 问题 联系我。如果问题与 Capsule 本身相关,请在 Laravel 官方仓库 上提交问题。

贡献

欢迎您通过 Github Pull Request 为此容器贡献新功能、修复或更新。