northern-lights/eloquent-bootstrap-php56

此包已被放弃,不再维护。作者建议使用 northern-lights/eloquent-bootstrap 包。

适用于独立 Eloquent ORM 的 Bootstrap(PHP5.6 版本)

1.0.2 2018-04-18 12:40 UTC

This package is auto-updated.

Last update: 2022-02-01 13:13:02 UTC


README

Maintainability

Laravel 中的 Eloquent ORM 使得与数据库交互变得极其简单。

不幸的是,如果您想独立使用它,而不是使用整个框架,事情就不再那么简单。

这个库为您解决了这个头疼的问题,并使用单条命令将 Eloquent ORM 带到您的项目中。

安装

通过 Composer

$ composer require northern-lights/eloquent-bootstrap

真的非常简单!

使用 - 单个连接

<?php

namespace NorthernLights\EloquentBootstrap\Example;

use NorthernLights\EloquentBootstrap\Database;
use NorthernLights\EloquentBootstrap\Provider\ConfigProvider;

require __DIR__ . '/vendor/autoload.php';

$database = new Database(new ConfigProvider([
        'host'     => 'localhost',
        'database' => 'database_name',
        'username' => 'user',
        'password' => 'pass'
]));

// At this point, eloquent will boot
$database->init();

使用 - 多个连接

<?php

namespace NorthernLights\EloquentBootstrap\Example;

use NorthernLights\EloquentBootstrap\Connection;
use NorthernLights\EloquentBootstrap\Database;
use NorthernLights\EloquentBootstrap\Provider\ConfigProvider;

require __DIR__ . '/vendor/autoload.php';

$database = new Database();
$database->addConnection(
    new Connection('first-database', new ConfigProvider([
        'host'     => 'localhost',
        'database' => 'first_database',
        'username' => 'user',
        'password' => 'pass'
    ]))
);

$database->addConnection(
    new Connection('second-database', new ConfigProvider([
        'host'     => 'localhost',
        'database' => 'second_database',
        'username' => 'user',
        'password' => 'pass'
    ]))
);

// At this point, eloquent will boot
$database->init();

您需要在您的引导文件中包含的所有内容。其他所有内容,请参阅 Eloquent 文档

注意:即使在这个示例中,您也可以通过数据库构造函数设置默认连接。

注意:NorthernLights\EloquentBootstrap\Database::getCapsule() 将返回 Capsule 实例,可以用来直接添加连接

创建一个简单的模型

<?php

namespace NorthernLights\EloquentBootstrap\Example;

use NorthernLights\EloquentBootstrap\Model as EloquentModel;

/**
 * Class Users
 * @package NorthernLights\EloquentBootstrap\Example
 */
class Users extends EloquentModel
{
    /** @var string  */
    protected $table = 'users';
}

注意 NorthernLights\EloquentBootstrap\Model 的使用,因为它只会修复 IDE 注释(已确认:PhpStorm)。它不包含任何逻辑。

PSR-2 标准

库力求符合 PSR-2 编码标准,因此我们包括了以下命令

$ composer check-style
$ composer fix-style

注意:第二个命令实际上会修改文件

PSR-4 标准

库符合 PSR-4 自动加载标准

测试

$ composer php-lint
$ composer test

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件