patrick-barreto/data-base
Composer 安装命令:
composer require patrick-barreto/data-base
包简介
Basic ORM to manipulate database and make a CRUD at model class very easy
README 文档
README
If you dont't have a php environment with a database, you can use this docker environment: https://github.com/PatrickBarreto/baseBackend
The next step of this code is:
- Implement the essentials DDL Commands (CREATE, DROP, TRUNCATE, ALTER)
If you have an idea or pull request to make, do it. Ideas are very welcome.
How to Install
composer require patrick-barreto/data-base
.env
You need to fill your environments variables. If you want make it easy use https://packagist.org/packages/patrick-barreto/dot-env
DotEnv\DotEnv::fill(PATH_.ENV_FILE);
How to Use
Repository
//Model class Users extends DataBaseCorrespondence { private static string $table = 'users'; public static function getTable(){ return self::$table; } public function getProperty($property){ return $this->$property; } } //Repository class UserRepository extends Repository{ public function findUsers() { return $this->select()->setFields(['id', 'name', 'email', 'phone']) ->fetchObject(false, $this->getDtoPath()); } } //Using in Controller for exemple class Users { use UserRepository; use Users; public function findUsers() { //This will be and instance of UserRepository, class responsable to provide methods of User Object. $usersRepository = new UserRepository(new Users); //This will return an instance of Users. $users = $usersRepository->findUsers(); } }
Use the Crud Instance
<?php namespace Your\NameSpace use DataBase\Crud; $homens = new Crud('homens'); $mulheres = new Crud('mulheres'); $animais = new Crud('animais'); $homens->insert->setFields(['name'])->setValues([["Pedro"],["João"],["Paulo"]])->runQuery(); $mulheres->insert->setFields(['name'])->setValues([["Maria"],["Ana"],["Marcia"]])->runQuery(); $animais->insert->setFields(['name'])->setValues([["Gato"],["Cachorro"],["Papagaio"]])->runQuery(); $mulheres->select->setFields(['homens.name as homemName, mulheres.name as mulherName']) ->setInnerJoin(['table' => 'homens'], ['table' => 'mulheres'] ) ->setWhere('homens.name = "João"');
Use the Command Instance
<?php namespace Your\NameSpace use DataBase\Actions\DML\Commands\Select; $select = new Select; $select->setTable('tableName'); $select->setWhereIn('nome', ['pedro', 'joão', 'josé']); $select->fetchAssoc(true);
Use the Query Class
IMPORTANT: Be carefull, here you controll what will be running. The application won't do nothing but execute the query.
<?php namespace Your\NameSpace use DataBase\Query; $sql = "SELECT * FROM teste"; $query = new Query; $result = $query->runQuery($sql)->fetchAll(PDO::FETCH_ASSOC);
Ressources for DML command Class
It is the same if instance manually or directilly with Crud Instance
Common for all down classes
This method is responsable to check if a table name was seted in the instance of class that heirded this class
public function getTableName()
This method is responsable to set a table in the instance of class that heirded this class
public function setTable(string $tableName)
This method is responsable to set the Where Simple Conditction for the query of instance class that heirded this class
public function setWhere(string $condition)
This method is responsable to set the Where In Conditction for the query of instance class that heirded this class
public function setWhereIn(string $column, array $options)
This method is responsable to concat the Inner Join command
public function setInnerJoin(array $joinedTable, array $newTableJoin)
This method is responsable to concat the Right Join command
public function setRightJoin(array $joinedTable, array $newTableJoin)
This method is responsable to concat the Left Join command
public function setLeftJoin(array $joinedTable, array $newTableJoin)
This method is responsable to concat the Full Join command
public function setFullJoin(array $newTableJoin)
This method is responsable to concat the Cross Join command
public function setCrossJoin(array $newTableJoin)
Select
DataBase\Actions\DML\Commands\Select
Set the distinct option, by default it is false
public function setDistinct(bool $requiereDistinct)
Set the fields option, by default its '*'
public function setFields(array $fields)
Set the limit option
public function setLimit(int $limit, int $offset = 0)
Set the order option, ASC or DESC
public function setOrder(string $fields, string $order = "ASC")
Set the group by option.
public function setGroupBy(array $fields)
Set the having option
public function setHaving(string $condition)
Build the query sentense for a Select query
public function buildQuery(bool $subquery = false)
This method id responsable to return data with an associative array
public function fetchAssoc(bool $returnAll = false)
This method is responsable to return data with an object of a class type. By default it is stdClass
public function fetchObject(bool $returnAll = false, string $class = stdClass::class)
Insert
DataBase\Actions\DML\Commands\Insert
Set the ignore option, by default it is false
public function setIgnore(bool $requiereIgnore)
Set the fields option, by default it is false
public function setFields(array $fields)
Set the values option, by default it is false
public function setValues(array $values)
Set the values with a SELECT query
public function setInsertSelect(string $query)
Build the query sentense for a Insert query
public function buildQuery()
This method is responsable to run the query of the class without a fetch result.
public function runQuery()
Update
DataBase\Actions\DML\Commands\Update
This method is responsable for set a SET command for an Update query
public function setSet(array $columnValue)
Build the query sentense for a Update query
public function buildQuery()
This method is responsable to run the query of the class without a fetch result.
public function runQuery()
Delete
DataBase\Actions\DML\Commands\Delete
Build the query sentense for a delete query
public function buildQuery()
This method is responsable to run the query of the class without a fetch result.
public function runQuery()
patrick-barreto/data-base 适用场景与选型建议
patrick-barreto/data-base 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 178 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 09 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 patrick-barreto/data-base 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 patrick-barreto/data-base 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 178
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2023-09-09