2017 © Pedro Peláez
 

library database

fdb

image

fastd/database

fdb

  • Saturday, October 22, 2016
  • by JanHuang
  • Repository
  • 4 Watchers
  • 3 Stars
  • 1,739 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 38 Versions
  • 1 % Grown

The README.md

FastD Database

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

好复杂..., (*2)

环境要求

  • PHP >= 7
  • fastd/generator >= 1.0.0
  • fastd/pagination >= 1.0.0

Composer

{
    "require": {
        "fastd/database": "~2.0.0"
    }
}

#

不开玩笑, 我觉得我真实太能折腾了, 明明有好用现成的, 还要自己操心., (*3)

数据表生成及操作对象映射结构如下:, (*4)

+------------+          +---------------+
|            |          |               |
|   Tables   |<-------->| SchemaBuilder |--------------+
|            |          |               |              v
+------------+          +---------------+       +-------------+          +--------------+
                                                |             |          |              |
                                                |   Schema    |-+------->| SchemaCache  |
                                                |             | |        |              |
+-----------+          +----------------+       +-------------+ |        +--------------+
|           |          |                |              ^        |
|    DB     |<-------->|  SchemaParser  |--------------+        |
|           |          |                |                       |
+-----------+          +----------------+                       |
                                                                |
                                                                |
                                                                |
                                                                |
                                                                v
                                                        +--------------+
                                                        |              |
                                                        | SchemaReflex |
                                                        |              |
                                                        +--------------+

1. 基础使用

1.1 配置
$fdb = new Fdb([
    "read" => [
        'database_host'      => '{host}',
        'database_port'      => '{port}',
        'database_name'      => '{dbname}',
        'database_user'      => '{user}',
        'database_pwd'       => '{pwd}'
    ],
    "write" => [
        'database_host'      => '{host}',
        'database_port'      => '{port}',
        'database_name'      => '{dbname}',
        'database_user'      => '{user}',
        'database_pwd'       => '{pwd}'
    ],
]);

即使在配置一个数据库链接的时候,都应该使用二维数组的形式进行配置注入., (*5)

获取驱动后可以进行最基础的操作, 因为驱动只是在 PDO 上封装了一层, 具体处理和 PDO 并无差别., (*6)

1.2 查询构建器
use FastD\Database\Query\MySQLQueryBuilder;

$queryBuilder = new MySQLQueryBuilder();

echo $queryBuilder->from('test'); // SELECT * FROM `test`;
echo $queryBuilder->select(['name'])->from('test'); // SELECT `name` FROM `test`
echo $queryBuilder->select(['name'])->from('test')->where(['name' => 'jan']); // SELECT `name` FROM `test` WHERE `name` = 'jan';

查询构建器仅限用于简单查询,不提供复杂的查询, (*7)

1.3 数据库连接驱动
$fdb->getDriver('read'); // return DriverInterface.
1.4 模型

模型的概念和普通框架的保持一致,可以手动新建。, (*8)

class DemoModel extends Model
{
    const TABLE = 'base';
}

手动创建的模型需要指定表名。, (*9)

可以通过 Reflex 进行映射。使用映射前,需要满足一下其中一个条件。, (*10)

  1. 已有数据库是否存在数据表
  2. 是否有构建数据schema。
use FastD\Database\Schema\SchemaParser;

$schemaDriver = new SchemaParser($driver);

$schemaDriver->getSchemaReflex()->reflex(
    __DIR__ . '/Reflex/' . $driver->getDbName(),
    'Test\\' . $driver->getDbName()
);

映射后会生成所有的实体对象,模型对象,因此会更加方便使用各个对象。, (*11)

use FastD\Database\Schema\SchemaBuilder;

$builder = new SchemaBuilder();

$testTable = new Table('test');

$builder->addTable($testTable);

$builder->getSchemaReflex()->reflex(
    __DIR__ . '/Reflex',
    'Test\\'
);

通过构建数据表 Schema 生成对象。使用方法和映射表一样,快速生成和构建实例对象。, (*12)

1.5 实体

实体需要通过映射生成,手动创建也可,但工序复杂。, (*13)

实体均继承 \FastD\Database\ORM\Entity。, (*14)

理解: 没个实体相当于对应一行具体的记录数, 通过 getter/setter 对其数据进行或去, 通过 toJson, toArray, toSerialize 方法对其进行结果集处理。, (*15)

而且实体对象实现 \ArrayAccess 对象, 所以在使用上和数组操作没有区别。, (*16)

$entity = new BaseEntity(DriverInterface, ['id' => 1]);

$entity->getName(); // joe
$entity->toArray(); // json_encode([], JSON_NUMERIC_CHECK);

以上操作原理上仅是执行了一个简单的数据库 key/value 查询操作,并且其本身没有过多关系和复杂的设计,因此可以保证在速度上不会有太大的损耗。, (*17)

1.6 Schema构建与数据填充

填充对象需要实现 FastD\Database\Fixtures\FixtureInterface 抽象接口,对应实现方法。, (*18)

namespace FastD\Database\Tests\Fixtures;

use FastD\Database\Fixtures\FixtureInterface;
use FastD\Database\Drivers\DriverInterface;
use FastD\Database\Schema\Structure\Field;
use FastD\Database\Schema\Structure\Table;
use Test\Rename\Dbunit\Entities\BaseEntity;

class DemoFixture implements FixtureInterface
{
    /**
     * Create schema
     *
     * @return Table
     */
    public function loadSchema()
    {
        $table = new Table('demo');

        $table->addField(new Field('id', Field::INT, 10));
        $table->addField(new Field('name', Field::VARCHAR, 20));
        $table->addField(new Field('nickname', Field::VARCHAR, 30));
        $table->addField(new Field('bir', Field::INT, 10));
        $table->alterField('nickname', new Field('nickname', Field::CHAR, 30));
        $table->dropField('bir');

        return $table;
    }

    /**
     * Fill DB data.
     *
     * @param DriverInterface $driverInterface
     * @return mixed
     */
    public function loadDataSet(DriverInterface $driverInterface)
    {
        $baseEntity = new BaseEntity($driverInterface);

        $baseEntity->setContent('test');

        $baseEntity->save();
    }
}

整个 fixture 对象需要先执行 loadSchema 方法,生成对应的实体对象, 才执行 loadDataSet 方法,对其进行数据填充。, (*19)

附言

望大家多多指点啦., (*20)

License MIT

The Versions

22/10 2016

2.0.x-dev

2.0.9999999.9999999-dev

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

22/10 2016

dev-master

9999999-dev

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

22/10 2016

v2.0.13

2.0.13.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

22/10 2016

v2.0.14

2.0.14.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

09/10 2016

v2.0.12

2.0.12.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

09/10 2016

v2.0.11

2.0.11.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

29/09 2016

v2.0.10

2.0.10.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

11/09 2016

v2.0.9

2.0.9.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

29/08 2016

3.0.x-dev

3.0.9999999.9999999-dev

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

29/08 2016

2.1.x-dev

2.1.9999999.9999999-dev

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

29/08 2016

v2.0.8

2.0.8.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

08/08 2016

v2.0.7

2.0.7.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

27/07 2016

v2.0.6

2.0.6.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

27/07 2016

v2.0.5

2.0.5.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

27/07 2016

v2.0.4

2.0.4.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

22/07 2016

v2.0.3

2.0.3.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

30/06 2016

v2.0.2

2.0.2.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

30/06 2016

v2.0.1

2.0.1.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

28/06 2016

v2.0.0

2.0.0.0

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

20/06 2016

v2.0.0-beta5

2.0.0.0-beta5

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

20/06 2016

v2.0.0-beta4

2.0.0.0-beta4

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

17/06 2016

v2.0.0-beta3

2.0.0.0-beta3

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

07/06 2016

v2.0.0-beta2

2.0.0.0-beta2

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

08/05 2016

v2.0.0-beta1

2.0.0.0-beta1

fdb

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

fastd fdb

26/12 2015

v0.2.6

0.2.6.0

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

24/11 2015

dev-dev

dev-dev

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

22/10 2015

v1.0.0-alpha3

1.0.0.0-alpha3

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

22/10 2015

v1.0.0-alpha2

1.0.0.0-alpha2

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

02/09 2015

v1.0.0-alpha1

1.0.0.0-alpha1

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

28/08 2015

v0.2.5

0.2.5.0

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

17/08 2015

v0.2.3

0.2.3.0

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

02/08 2015

v0.2.2

0.2.2.0

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

02/08 2015

v0.2.4

0.2.4.0

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

02/08 2015

v0.2.1

0.2.1.0

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

01/08 2015

v0.2.0

0.2.0.0

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

14/07 2015

v0.1.2

0.1.2.0

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

23/06 2015

v0.1.1

0.1.1.0

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd

20/06 2015

v0.1.0

0.1.0.0

FastD php simple framework database driver component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

database fastd