oza/database

处理您数据的数据库包

0.0.2 2019-04-03 20:17 UTC

This package is auto-updated.

Last update: 2024-09-04 21:39:54 UTC


README

处理您数据的数据库包

  • 完全与 Mysql 驱动程序测试通过
  • 需要贡献者
  • 无任何依赖制作

安装

composer require oza/database

用法

首先在 应用程序根目录(重要) 中创建一个包含您的数据库配置的配置文件

  • 配置文件示例
<?php
 
 return [
     'driver' => 'mysql',
     'name' => 'db_package',
     'host' => 'localhost:3306',
     "username" => "root",
     "password" => "",
     "migrations" => [
         "folder" => __DIR__ . '/../migrations/' // path of migrations folder ( #important )
     ]
 ];
  • 然后注册您的 composer 自动加载器
   <?php
   // After registered your composer autoloader (e.g: require_one __DIR_."/../vendor/autoload.php"
   // Register your config files
   \OZA\Database\Config::instance()->register(__DIR__.'/../db.php');
   

配置类是一个 单例设计模式),因此您可以在应用程序的任何地方注册所有您想要的配置文件并访问它们。

您可以在这里找到其文档

迁移

迁移就像是数据库的版本控制,允许您的团队能够轻松地修改和共享应用程序的数据库模式。迁移通常与模式构建器配对,以便轻松构建应用程序的数据库模式。如果您曾经告诉团队成员手动添加列到他们的本地数据库模式中,您就面临了数据库迁移解决的问题。

  • 如何创建迁移

    • 打开您的终端并运行

          php vendor/bin/database migration:create {name_of_migrations} --table={name_of_table_you_want_to_create}
    • 例如,如果我想为我的应用程序创建一个用户表

        php vendor/bin/database migration:create create_users_table --table=users

    此命令将在您在配置文件中指定的迁移文件夹中创建一个新的迁移文件

    <?php
    
    use OZA\Database\Migrations\Interfaces\MigrationTableInterface;
    use OZA\Database\Migrations\Schema\Schema;
    use OZA\Database\Migrations\Table;
    
    class CreateTestUsersMigration implements MigrationTableInterface
    {
    
        /**
         * Create table
         *
         * @param Schema $schema
         * @return mixed
         */
        public function up(Schema $schema): void
        {
            $schema->create('users', function (Table $table) {
    
                $table->integer('id')->autoIncrement()->index();
    
                $table->timestamps();
            });
        }
    
        /**
         * Called when rollback
         *
         * @param Schema $schema
         * @return mixed
         */
        public function down(Schema $schema): void
        {
            $schema->drop('users');
        }
    }

    如果您熟悉 Laravel,此文件看起来像 laravel 迁移,有一些小小的区别。我使用过 Laravel,并且我喜欢他们使事情变得简单的方式。

    up 方法在您想要创建表时调用,而 down 在您想要回滚时调用。

    up 方法的 schema->create 闭包中,您可以创建所有类型的列。例如,我们将向表中添加 nameemailpassword

    <?php
    
    use OZA\Database\Migrations\Interfaces\MigrationTableInterface;
    use OZA\Database\Migrations\Schema\Schema;
    use OZA\Database\Migrations\Table;
    
    class CreateTestUsersMigration implements MigrationTableInterface
    {
    
        /**
         * Create table
         *
         * @param Schema $schema
         * @return mixed
         */
        public function up(Schema $schema): void
        {
            $schema->create('users', function (Table $table) {
    
                $table->integer('id')->autoIncrement()->index();
                $table->string('name')->index();
                $table->string('email')->index()->unique();
                $table->string('password');
                $table->timestamps();
            });
        }
    
        /**
         * Called when rollback
         *
         * @param Schema $schema
         * @return mixed
         */
        public function down(Schema $schema): void
        {
            $schema->drop('users');
        }
    }
    - See [Column types]()
    - See [Column Definitions]()
    
  • 当您对您的迁移文件满意时,将它们提交到数据库

    在您的终端中

     php vendor/bin/database migrate
  • 如果您想要回滚

    在您的终端中

     php vendor/bin/database migrate:rollback

    这将回滚您提交的最新迁移

ORM

该包还附带了一个小的 ORM

要检索表中的信息,您可以使用 QueryBuilder 类或 Query Facade

    <?php
    $user = \OZA\Database\Facade\Query::table('users')->find(2);

表上有许多方法(whereorWherewhereInorWhereIngetfirstlimit 等...),这有助于您检索数据。您可以在这里找到 QueryBuilder 文档

您还可以创建一个 UserEntity,它将代表您的用户

<?php
<?php


namespace OZA\Database\Tests\Entities;


use OZA\Database\Query\Entity;
use OZA\Database\Query\Relations\ManyToOne;

class UserEntity extends Entity
{

    /**
     * @param string $value
     * @return mixed
     */
    public function getEmailAttribute(string $value)
    {
        return strtoupper($value);
    }

    public function setNameAttribute($value)
    {
        return strtoupper($value);
    }

    /**
     * @return ManyToOne
     */
    public function posts()
    {
        return $this->manyToOne(PostEntity::class, 'user_id', 'id');
    }
}

然后您可以像这样获取用户

<?php
// Create new User
\App\Entities\UserEntity::make([
    'name' => 'Aboubacar',
    'email' => 'user@email.com'
])->create();

// Find a User
\App\Entities\UserEntity::query()->find(2);

// Count
 \App\Entities\UserEntity::query()->count();
 
 // With Where clause
 \App\Entities\UserEntity::query()->where('name', 'aboubacar')->get();
 
 // First item
 \App\Entities\UserEntity::query()->where('name', 'LIKE', '%bouba%')->first();
 
 \App\Entities\UserEntity::query()->where(function (\OZA\Database\Query\QueryBuilder $builder) {
    $builder->where('name', 'aboubacar')
    ->orWhere('name', 'oza')
    ->get(); 
 });

QueryBuilder 有很多方法,如果您已经使用过 Laravel,只需像 Laravel QueryBuilder 一样使用它。