erickfirmo/phpmodel
Composer 安装命令:
composer require erickfirmo/phpmodel
包简介
MySQL query builder class in PHP.
关键字:
README 文档
README
MySQL Query Builder in PHP - by Erick Firmo - https://erickfirmo.dev
Requirements
- PHP >= 7.4
Install
Install with composer:
composer require erickfirmo/phpmodel
Namespace
<?php use ErickFirmo\Model;
Usage example
<?php // Requires composer autoloader require __DIR__ . '/vendor/autoload.php'; use ErickFirmo\Model; // Creating a class for the entity class Car extends Model { protected $table = 'cars'; protected $fillable = [ 'name', 'company', 'year', 'plate', 'color' ]; } // Insert register example, returns boolean $saved = (new Car())->insert([ 'name' => $name, 'company' => $company, 'year' => $year, 'plate' => $plate, 'color' => $color, ]); // Select register example, returns collection $cars = (new Car())->select() ->where('year', '=', $year) ->get();
Collections
{
"model": "App\\Models\\Car",
"table": "cars",
"attributes": [
"id",
"name",
"company",
"year",
"plate",
"uf",
"color",
"price"
],
"items": [
{
"id": "12",
"name": "Fusca",
"company": "VW",
"year": "1934",
"plate": "ERX-8761",
"uf": "SP",
"color": "yellow",
"price": "89000"
},
{
"id": "13",
"name": "Uno",
"company": "Fiat",
"year": "1934",
"plate": "ERX-8761",
"uf": "SP",
"color": "red",
"price": "89000"
},
{
"id": "14",
"name": "Chevette",
"company": "Chevrolet",
"year": "1934",
"plate": "ERX-8761",
"uf": "SP",
"color": "black",
"price": "89000"
},
],
"pages": [
],
}
Query
Methods that facilitate the execution of mysql queries in the database:
Select
Select all columns from the table using the select method. Use the get method to perform a query and return a collection:
<?php // Returns all columns from `cars` table $cars = (new Car())->select() ->get();
Select specific columns from the table passing an array as parameter in select method. Use the get method to perform a query:
<?php // Returns specific columns from model table $cars = (new Car())->select(['name', 'company', 'year']) ->get();
Where
Adding where clause to query builder:
<?php $cars = (new Car())->select() ->where('company', '=', $company) ->get();
Adding multiple where clause to query builder:
<?php $cars = (new Car())->select() ->where('company', '=', $company) ->where('year', '=', $year) ->get();
Insert
Inserting record into database table:
<?php $saved = (new Car())->insert([ 'name' => $name, 'company' => $company, 'plate' => $plate, 'year' => $year, 'color' => $color, ]);
Update
Updating register into database table:
<?php $saved = (new Car())->update($id, [ 'plate' => $plate, 'color' => $color, ]);
Delete
Deleting register into database table:
<?php $saved = (new Car())->delete($id);
FindById
Searching register by id:
<?php $car = (new Car())->findById($id);
OrderBy
You can configure the ordering as ascending or descending using the words asc or desc as parameter in orderBy method.
Ordering as ascending:
<?php $cars = (new Car())->select() ->orderBy('asc') ->get();
Ordering as descending:
<?php $cars = (new Car())->select() ->orderBy('desc') ->get();
Limit
Limiting number of records in the query:
<?php $cars = (new Car())->select() ->limit(50) ->get();
Pagination
We can paginate records using the paginate method. We must pass the desired number of records per page as a parameter. This method has a value of 10 by default.
In this example, we have 100 records, and we'll display 25 per page:
<?php $cars = (new Car())->select() ->paginate(25);
By default, the pages attribute of the collections will be an array with the number of pages:
"pages": [ 1, 2, 3, 4 ],
We can use this array to create our paging component. Simple example of page component in with php and bootstrap:
<nav aria-label="Page navigation example">
<ul class="pagination">
<?php foreach ($cars->pages as $key => $page) { ?>
<li class="page-item <?php echo (!isset($_GET['page']) && $page == 1) || $_GET['page'] == $page ? 'active' : ''; ?>">
<a class="page-link" href="<?php echo 'pessoas?page='.$page; ?>">
<?php echo $page; ?>
</a>
</li>
<?php } ?>
</ul>
</nav>
erickfirmo/phpmodel 适用场景与选型建议
erickfirmo/phpmodel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「php」 「sql」 「db」 「mysql」 「mvc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 erickfirmo/phpmodel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 erickfirmo/phpmodel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 erickfirmo/phpmodel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Query filtering in your frontend
A PSR-7 compatible library for making CRUD API endpoints
Database/ORM-agnostic query construction helper
统计信息
- 总下载量: 47
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2020-01-26