madesimple/database

数据库实体和查询构建器

v0.0.1 2017-03-24 11:30 UTC

This package is auto-updated.

Last update: 2024-08-27 08:24:13 UTC


README

Build Status

数据库包是PHP和SQL数据库之间的抽象层。该包的主要功能包括

  1. 迁移控制
  2. 数据库填充
  3. 查询构建
  4. 实体和关系

迁移控制

您可以通过一个易于使用、可靠的命令行来控制数据库的迁移。可能的操作包括安装迁移表、升级、回滚、卸载和刷新。这些操作都可以通过bin/database调用。

例如,第一次克隆使用数据库的包

> composer install
...
> vendor/bin/database database:install -e -v
[notice] Migration table created
> vendor/bin/database database:upgrade -ep examples/migrations -v
[notice] Migrated file: "/path/to/database/examples/migrations/v1.0.0-ExampleInitial.php"
[notice] Migrated file: "/path/to/database/examples/migrations/v1.0.1-ExampleComment.php"

或者您也可以使用快捷键

> composer install
...
> vendor/bin/database database:migrate -e -p examples/migrations -v
[notice] Migration table created
[notice] Migrated file: "/path/to/database/examples/migrations/v1.0.0-ExampleInitial.php"
[notice] Migrated file: "/path/to/database/examples/migrations/v1.0.1-ExampleComment.php"

例如,回滚迁移

> vendor/bin/database database:rollback -e -v
Rolling back batch: 1
[notice] Rolled back file: "/path/to/database/examples/migrations/v1.0.1-ExampleComment.php"
[notice] Rolled back file: "/path/to/database/examples/migrations/v1.0.0-ExampleInitial.php"

例如,卸载

> vendor/bin/database database:uninstall -e -v
[notice] Migration table removed

例如,刷新

> vendor/bin/database database:refresh -e -p examples/migrations/ -s examples/seeds/ -v
[notice] Migration table already installed
[notice] Rolled back file: "/path/to/database/examples/migrations/v1.0.1-ExampleComment.php"
[notice] Rolled back file: "/path/to/database/examples/migrations/v1.0.0-ExampleInitial.php"
[notice] Migrated file: "/path/to/database/examples/migrations/v1.0.0-ExampleInitial.php"
[notice] Migrated file: "/path/to/database/examples/migrations/v1.0.1-ExampleComment.php"
[notice] Seeded file: "/path/to/database/examples/seeds/v1.0.0-ExampleTableSeeder.php"

数据库填充

您可以为数据库创建填充,这些填充用于在数据库中填充示例数据。这可以通过bin/database调用。

例如,对数据库进行填充

> vendor/bin/database database:seed -e -v -s examples/seeds
[notice] Seeded file: "/path/to/database/examples/seeds/v1.0.0-ExampleTableSeeder.php"

查询构建

选择

$connection
    ->select()
    ->from('table')
    ->where('column', '=', 'value');
// SELECT * FROM `table` WHERE `columns` = ?

更新

$connection
    ->update()
    ->table('table')
    ->set('column', 'new-value')
    ->where('another-column', '=', 'value');
// UPDATE `table` SET `column`=? WHERE `another-column` = ?

插入

$connection
    ->insert()
    ->into('table')
    ->columns('column1', 'column2')
    ->values(5, 'value');
// INSERT INTO `table` (`column1`,`column2`) VALUES (?,?)

$rows = [
    ['column1_value1', 'column2_value1'],
    ['column1_value2', 'column2_value2'],
    ['column1_value3', 'column2_value3'],
];
$connection
    ->insert()
    ->into('table')
    ->columns('column1', 'column2')
    ->chunkedQuery($rows, 2);
// INSERT INTO `table` (`column1`,`column2`) VALUES (?,?),(?,?)
// INSERT INTO `table` (`column1`,`column2`) VALUES (?,?)

删除

$connection
    ->delete()
    ->from('table')
    ->where('column', '=', 'value');
// DELETE FROM `table` WHERE `column` = ?

语句

$connection->statement(function (CreateTable $create) {
    $create->table('user')->ifNotExists();
    $create->column('id')->integer(10)->primaryKey()->autoIncrement();
    $create->column('uuid')->char(36)->unique()->notNull();
    $create->column('email')->char(255)->unique()->notNull();
    $create->column('password')->char(255)->notNull();
    $create->column('created_at')->timestamp()->notNull()->useCurrent();
    $create->column('updated_at')->timestamp()->notNull()->useCurrent();
    $create
        ->engine('InnoDB')
        ->charset('utf8mb4', 'utf8mb4_general_ci');
});
// CREATE TABLE IF NOT EXISTS `user` (
//   `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
//   `uuid` CHAR(36) NOT NULL UNIQUE,
//   `email` CHAR(255) NOT NULL UNIQUE,
//   `password` CHAR(255) NOT NULL,
//   `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
//   `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
// ) ENGINE=InnoDB,DEFAULT CHARACTER SET=utf8mb4,COLLATE=utf8mb4_general_ci

实体和关系

class User extends \MadeSimple\Database\Entity
{
    use \MadeSimple\Database\Entity\Relational;

    protected static function getMap()
    {
        return new \MadeSimple\Database\EntityMap(
            'user', // Table name
            ['id'], // Primary key(s)
            [       // Other columns: database name => property name
                'uuid',
                'email',
                'password',
                'created_at' => 'createdAt',
                'updated_at' => 'updatedAt',
            ]
        );
    }

    /**
     * @return \MadeSimple\Database\Relationship\ToMany
     */
    public function posts()
    {
        return $this->toMany()->has(Post::class, 'p', 'user_id');
    }
}
class Post extends \MadeSimple\Database\Entity
{
    use \MadeSimple\Database\Entity\Relational;

    protected static function getMap()
    {
        return new \MadeSimple\Database\EntityMap(
            'post', // Table name
            ['id'], // Primary key(s)
            [       // Other columns: database name => property name
                'uuid',
                'user_id' => 'userId',
                'title',
                'content',
                'created_at' => 'createdAt',
                'updated_at' => 'updatedAt',
            ]
        );
    }

    /**
     * @return \MadeSimple\Database\Relationship\ToOne
     */
    public function user()
    {
        return $this->toOne()->belongsTo(User::class, 'u', 'user_id');
    }
}

支持的数据库

当前支持的SQL数据库有

  • MySQL
  • SQLite

外部文档

外部依赖的文档链接

仅开发的外部依赖文档链接