mfurman / pdomodel
这是一个PDO模型表示的库
This package is auto-updated.
Last update: 2024-09-19 01:33:44 UTC
README
PDO访问类与PDO模型 - 版本1.0.3
从版本1.0.3的变化
- 代码版本1.0.2的微小修正,
- 接口类中的微小修正,
- 添加了简单的日志方法,
- 添加了对PDO单例实例的检查,
- 变量命名对齐,
从版本1.0.2的变化
- 更改了模型中的数据库表声明 - 现在您必须将表声明为PDOAccess实例后的父属性构造函数中,
- 更改了模型中的执行受保护方法 - 现在您不需要在执行方法中设置表名作为参数 - 表由构造函数声明,
- 添加了两个公共方法 - "set_table" 和 "get_table" - 这些方法是构造函数中声明的表名称更改的setter和getter(新方法),
- 更改了执行公共方法 "exist" 的方式 - 现在您可以将此方法的表设置为最后一个调用参数,如果没有声明此参数,则方法将在构造函数声明的存在表中执行,
简单安装
只需使用composer安装新的依赖项:composer require mfurman/pdomodel "1.0.3"
简单使用
首先设置MySQL数据库的配置(可以是简单的数组)
$config = [ 'DB_CONNECTION' => 'mysql', 'DB_HOST' => 'localhost', 'DB_PORT' => '3306', 'DB_DATABASE' => 'testdb', 'DB_USERNAME' => 'access', 'DB_PASSWORD' => 'password', 'LOGGER_DIR' => 'logger directory started from base directory of project (default "./errors")', ];
然后通过执行静态方法GET初始化PDOAccess单例实例
\mfurman\pdomodel\PDOAccess::get($config);
此时您在您的应用程序中有一个PDO访问类的实例,您可以在任何时候获取此实例。
现在您可以通过扩展dataDb类(此类具有特定模型的状态)来简单地创建模型
模型示例
use \mfurman\pdomodel\PDOAccess; use \mfurman\pdomodel\dataDb; class User extends dataDb { private $users_table = 'users'; public function __construct($commit=true) { parent::__construct(PDOAccess::get(), $this->users_table, $commit, false); } // return associative array, table of users public function getAll() :array { return $this->read('*')->get(); } // return associative array, users data public function getById(int $id) :array { return $this->read('*','id = '.$id)->get(); } // return associative array, users data public function getByName(string $name) { return $this->read('*','user_name = "'.$name.'"')->get(); } // return id of new row, public function add(array $data) :int { $this->set(array('user_name'=>$data['user_name'])); return $this->insert(); } // return true or false public function updateOne(array $data, int $id) :bool { if (empty($this->read('*','id = '.$id)->get())) return false; $this->set(array('user_name'=>$data['user_name'])); $this->update($id); return true; } public function deleteById(int $id) :bool { if (empty($this->read('*','id = '.$id)->get())) return false; $this->delete_where('id = '.$id); return true; } }
当您创建模型时,您可以创建一些存储库。在存储库中,您可以对数据库事务和提交进行控制。
存储库示例
use Myvendor\Myapp\Models\Task; use Myvendor\Myapp\Models\User; class TaskRepository { private $task; private $user; (...) public function store(array $data) :array { $this->task = new Task(false); // - set autocommit to false (true/false) $this->user = new User(false); // - set autocommit to false (true/false) // inform PDO about start transaction $this->task->begin(); $this->user->getByName($data['user_name']); if ($this->user->is()) $data['user_id'] = $this->user->get('id'); else { $this->user->reset(); $data['user_id'] = $this->user->add($data); } $id = $this->task->add($data); // inform PDO to commit ealier started transaction, if something go wrong - commit will rollback $this->task->commit(); // this toggle change model to autocommit transactions (true/false) $this->task->set_commit(true); return $this->task->getById($id); }
Class dataDb具有构建的方法,我们可以在我们的模型或存储库中使用这些方法。
公共方法(可以在模型和存储库中使用)
构造函数 - 参数:(PDOModel-singletone,数据库表,提交,是否将任何保存到数据库中的注册到日志表中)
parent::__construct(object $dbaccess, $this->myTable, $commit=true, $log_rec=false);
如果您想在日志表中注册活动,您必须创建'general_log'表(请参阅末尾)。如果不这样做,只需将此切换设置为'false'或不要使用它 - 默认为'false'。
set_commit - 切换以更改自动提交 - 默认为'true'。
$this->set_commit(bool $flag=true);
reset - 重置模型的状态,不会重置表名
$this->reset();
set_table - 更改或设置模型数据库表(新方法)
$this->set_table(string $table);
get_table - 返回模型中设置的当前数据库表名(新方法)
$this->get_table();
is - 通知状态 - 是否已填充 - 返回'true/false'。
$this->is();
set - 设置模型中的数据。
$this->set('name','value'); //or we can set multiple $this->set(array( 'name01' => 'value01' 'name02' => 'value02' 'name03' => 'value03' ) );
get - 从模型中获取按名称的值。
$this->get('name'); //or if is multiple data table - for eg. table of 'users' $this->get('name',0); // - get name of first user
get_flat - 获取特定数据的数组作为一维数组。
$this->get_flat();
del - 通过名称从状态中删除某些值。
$this->del('name');
begin - 当autocommit为false时开始事务。
$this->begin();
commit - 当autocommit为false时提交开始的事务。
$this->commit();
受保护的方法(只能在模型中使用)
read - 从表中读取数据,填充模型状态 - 执行(提交) - 返回模型对象,可以是链式调用。
$this->read(table, columns, where, order, limit) :object
$this->read('*', 'name = "John"') :object // or $this->read(['ID', 'name', 'surname', 'city'], 'name = "John" AND age > 23', 'DESC', 1) :object
insert - 将数据插入到数据库表中 - 执行(提交) - 返回新创建行的ID。
$this->insert() :int
更新 - 更新表中现有行数据 - 执行(提交) - 返回模型对象,可进行链式调用
$this->update($id) :object
update_where - 更新表中现有行数据 - 执行(提交) - 返回模型对象,可进行链式调用
$this->update_where('name = "John"') :object
delete_where - 从表中删除数据 - 执行(提交) - 返回模型对象,可进行链式调用
$this->delete_where('name = "John"') :object
exists - 检查数据是否存在于数据库中 - 返回true/false
$this->exists(table,['name1' => 'value1', 'name2' => 'value2', ...], where(in SQL), table (可选))
$this->exists(['name' => 'John', 'surname' => 'Smith']) :bool //or $this->exists(['name' => 'John', 'surname' => 'Smith'], 'ID != '.$id, $this->otherTable) :bool
通用日志表结构
当我们在用户和服务器之间设置会话时,使用通用日志表。它使用会话数据来识别现有用户及其活动。我们必须为已登录用户设置两个会话数据
$_SESSION['user_id'] = 12 // set when user log in - for eg ID from users table, $_SESSION['user_name'] = 'John Smith' // set when user log in - username from users table,
通用日志表必须具有以下特定列和名称
表名: 'general_log'
列
- 'date': 时间戳,
- 'user_id': int - $_SESSION['user_id']
- 'user_name': string - $_SESSION['user_name']
- 'method': string - (在数据库上执行的方法,如INSERT, UPDATE, DELETE)
- 'value': string - (用户生成的SQL查询)
当标志'log_rec'设置为'true'时,通用日志中的所有数据将自动填充。
享受。
许可证 🗝️
根据许可证(MIT, Apache等)
MIT © Michał Furman