nimayneb / repop
Repository Operator - 在模型上下文中使用 PDO 执行 CRUD 操作的小型库。
2.0.0
2020-04-15 22:11 UTC
Requires
- php: ^7.4
- ext-pdo: ^7.2
- ext-pdo_mysql: ^7.2
- doctrine/dbal: ^2.10
- helhum/dotenv-connector: ^2.1
This package is auto-updated.
Last update: 2024-09-16 12:40:54 UTC
README
数据库连接 (.env 支持)
使用 .env
文件准备
DATABASE_DRIVER=mysql
DATABASE_NAME=test
DATABASE_HOSTNAME=localhost
DATABASE_USERNAME=admin
DATABASE_PASSWORD=secret
DATABASE_PORT=3306
在 application.php
中的使用(包含 .env
)
$connection = JayBeeR\Repop\Connector\DatabaseFactory::connectToDatabase();
在 application.php
中的使用(不包含 .env
)
$connection = JayBeeR\Repop\Connector\DatabaseFactory::connectToDatabase(
'mysql',
'localhost',
3306,
'test',
'admin',
'secret'
);
使用当前连接注册仓库
给定的类必须扩展自抽象类 JayBeeR\Repop\Repository\RepositoryAttributes。
使用
$connection = JayBeeR\Repop\Connector\DatabaseFactory::connectToDatabase();
$connection->registerRepository({Repository model class}::class);
使用分配的连接使用 Repository
使用
$repository = Repository::getRepository('{unique table name for repository}');
创建 Repository
如果名称不明确(如单数形式),使用 "s"、"Container" 作为后缀。
模板
class {model name in plural} extends JayBeeR\Repop\Repository\RepositoryObject
{
}
设置仓库的表名
使用
/**
* @var string
*/
protected static $tableName = '{table name}';
指定模型对象的属性
使用
/**
* @return array
*/
protected function getTableColumns(): array
{
return {static model class}::getTableColumns();
}
指定模型对象的创建
使用
/**
* @param PDOStatement $statement
*
* @return {model class}
*/
protected function toObject(PDOStatement $statement): {model class}
{
return {static model class}::fromResult($statement);
}
定义一个公共查询方法
约定
- 说出你想要查找的内容: "findElementWithLove" => 返回 PDOStatement(可重复使用)
- 说出你想要获取的内容: "elementWithLove" => 返回 Generator(不可重复使用,使用
foreach
)
(作为 Generator)使用
/**
* @return Generator
*/
public function elementWithLove(string $value): Generator
{
$statement = $this->getConnection()->prepare(
"select {$this->buildTableColumnsStatement()}
from `{$this->getTable()}`
where `column_name` = :columnName;"
);
$statement->bindParam(':columnName', $value, PDO::PARAM_STR);
$this->ensureStatementExecution($statement);
return $this->iterate($statement);
}
(可重复使用)使用
/**
* @return PDOStatement
*/
public function elementWithLove(string $value): PDOStatement
{
$statement = $this->getConnection()->prepare(
"select {$this->buildTableColumnsStatement()}
from `{$this->getTable()}`
where `column_name` = :columnName;"
);
$statement->bindParam(':columnName', $value, PDO::PARAM_STR);
$this->ensureStatementExecution($statement);
return $statement;
}
创建模型类
如果名称不明确(如复数形式),使用 "Single"、"Item"、"Piece" 作为后缀。
模板
class {model name in singular} extends JayBeeR\Repop\Model\ModelObject
{
}
添加新的列属性
约定
- 使用下划线(如 SQL)
- 正确:
$column_name;
- 错误:
$columnName;
- 正确:
- 必须是受保护的属性
可能的数据类型
- 字符串
- 浮点数
- 整数
- 布尔值
使用
/**
* {Description}
*/
protected {column type} ${column name};
为列属性添加 Getter 方法
约定
- 使用驼峰式和更易理解的列名称
- 不是 "getUid" => "getIdentifier"
- 不是 "getTxExtensionLock" => "getLockState"
可能的数据类型
- 字符串
- 浮点数
- 整数
- 布尔值
使用
/**
* @return {column type}
*/
public function get{colum name}(): {column type}
{
return $this->{colum name};
}
为列属性添加 Setter 方法
约定
- 使用驼峰式和更易理解的列名称
- 不是 "setUid" => "setIdentifier"
- 不是 "setTxExtensionLock" => "setLockState"
使用
/**
* @param {column type} $value
*/
public function set{column name}({column type} $value): void
{
$this->updateColumnValue(static::{column name}, $value);
}
添加静态方法通过 PDOStatement 创建此模型类
使用
/**
* @param PDOStatement $statement
*
* @return {ModelClass}|null
*/
public static function fromResult(PDOStatement $statement): ?{ModelClass}
{
return static::get($statement);
}