osemk/php-custom-model
Composer 安装命令:
composer require osemk/php-custom-model
包简介
PHP CustomModel let's you to convert your MYSQL table automaticly to a PHP Model
README 文档
README
PHPCustomModel let's you to convert your MYSQL database tables to a PHP Model automaticly. This library uses mysqli to connect and to request database.
FAQ
- Can I run PHPCustomModel for all MYSQL tables?
- I tried to adopt it to use for all MYSQL tables but it can gives error with tables with non-PRI keys or tables has less identifier columns.
Getting Started
Before you start using this Library, you need to know how PHP works, you need to know how MYSQL work and what is Models. This is a fundamental requirement before you start. Without this knowledge, you will only suffer.
Requirements
Installing PHPCustomModel
PHPCustomModel is installed using Composer.
- Run
composer require osemk/php-custom-model. This will install the latest stable release. - Include the Composer autoload file at the top of your main file:
include __DIR__.'/vendor/autoload.php';
- Make models!
Basic Example
<?php include __DIR__.'/vendor/autoload.php'; use CustomModel\CustomModel; define("VT_HOST","localhost"); // database host define("VT_ADI","databasename"); //database name define("VT_KULLANICI","databaseuser"); //databae user define("VT_SIFRE","databasepassword"); //database password $model = new CustomModel(TABLENAME,ID);
Documentation
PHPCustomModel converts tables to models. A sample database table that name is "employees"
| id | firstname | jobtitle |
|---|---|---|
| 1 | Diane | President |
| 2 | Mary | VP Sales |
| 3 | Jeff | VP Marketing |
| 4 | William | Sales Manager (APAC) |
| 5 | Gerard | Sale Manager (EMEA) |
Using PHPCustomModel we can easily convert this table to a model and we can easily do CRUD processes on table like below.
Bring table
$employee = new CustomModel("employees", 1);
This will fetch row with ID =1 and assign columns to $employee var.
We can check if has a record with ID=1,
Check records
$employee->hasRecord(); // if true id=1 founded, if false there is no record.
Now the current object includes these;
id=1, firstname=Diane, jobtitle=President
We can change these parameters using model easily like below;
Change parameters
$employee->firstname = "Angel";
$employee->jobtitle = "New President";
or
$employee->veri['firstname'] = "Angel";
$employee->veri['jobtitle'] = "New President";
and update it;
Update
$employee->update();
Now our table looks like below;
| id | firstname | jobtitle |
|---|---|---|
| 1 | Angel | New President |
| 2 | Mary | VP Sales |
| 3 | Jeff | VP Marketing |
| 4 | William | Sales Manager (APAC) |
| 5 | Gerard | Sale Manager (EMEA) |
That is it!
If you want to delete record, use;
Delete
$employee->delete();
Now our table;
| id | firstname | jobtitle |
|---|---|---|
| 2 | Mary | VP Sales |
| 3 | Jeff | VP Marketing |
| 4 | William | Sales Manager (APAC) |
| 5 | Gerard | Sale Manager (EMEA) |
That is awesome!
Insert New record
Inserting a new row is also easy, just don't send an ID like below,
$employee = new CustomModel("employees",0);
It creates new model from scracth. Now you can add some strings
$employee->firstname = "Onur";
$employee->jobtitle = "King of the World";
$employee->insert();
Now our table look like below;
| id | firstname | jobtitle |
|---|---|---|
| 2 | Mary | VP Sales |
| 3 | Jeff | VP Marketing |
| 4 | William | Sales Manager (APAC) |
| 5 | Gerard | Sale Manager (EMEA) |
| 6 | Onur | King of the World |
Other fetching methods
You can fetch directly using other columns except ids.
$employee = new CustomModel("employees",['firstname' => 'Onur']); it directly fetches id=6 but this column is not an identifier may be there are some records too, so it fetches only latest "Onur" record.
So you can make more unique your request with adding extra columns like below;
$employee = new CustomModel("employees",['firstname' => 'Onur', 'jobtitle' => 'King of the World']);
Other easy method
The best method for this library is save() method. When you want to change or insert a record to table, you can use save().
$employee = new CustomModel("employees", 7);
$employee->firstname = "Ali";
$employee->jobtitle = "Amele";
$employee->save();
// if there is id=7 currently it updates record 7; if there is no id=7 record, it inserts automaticly to id=7.
So awesome, didn't it?
License
MIT License, © Onur Erginer 2023-present.
osemk/php-custom-model 适用场景与选型建议
osemk/php-custom-model 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 osemk/php-custom-model 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 osemk/php-custom-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-09