basecode/querymodel
Composer 安装命令:
composer require basecode/querymodel
包简介
QueryModel Component...
关键字:
README 文档
README
What's the QueryModel ?
Abstract: QueryModel is a standalone component for inserting, updating, reading and deleting data in the database. Recommended for projects with mvc structure.
Getting Started
Installation
You can install the QueryModel in your project with composer.
Just run the command below on your terminal:
composer require basecode/querymodel
or in your composer.json require:
"basecode/querymodel": "1.1.*"
Usage
For the correct functioning of the QueryModel it is necessary that you create a constant named DB_CONFIG with the access data of the database. See the example below.
Example config.php file:
<?php define("DB_CONFIG", [ "driver" => "mysql", "host" => "localhost", "name" => "querymodel", "user" => "root", "password" => "", "options" => [ PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ ] ]);
Example of use index.php file:
<?php date_default_timezone_set("America/Sao_Paulo"); define("DS", DIRECTORY_SEPARATOR); require_once dirname(__DIR__).DS."vendor".DS."autoload.php"; require_once dirname(__FILE__).DS."config".DS."Config.php"; use BaseCode\QueryModel\QueryModel; Class User extends QueryModel { protected $table = "users"; // protected $primary = "id"; protected $required = [ "name", "email", "password" ]; protected $timestamp = [ "create" => "created_at", "update" => "updated_at" ]; public function posts() { return $this->select("users.name, posts.title") ->join("INNER JOIN posts ON users.id = posts.id_user") ->where("users.id = :id") ->params(["id" => $this->id]) ->execute(); } public function usersPosts() { return $this->select("users.id, users.name, posts.title") ->join("INNER JOIN posts ON users.id = posts.id_user") ->execute(); } } Class Post extends QueryModel { protected $table = "posts"; // protected $primary = "id"; protected $required = [ "id_user", "title", "message" ]; protected $timestamp = [ "create" => "created_at", "update" => "updated_at" ]; } $user = new User(); $post = new Post(); echo "<pre>"; // insert register $user->name = "test"; $user->email = "test@gmail.com"; $user->password = md5("123"); $user->save(); // select all print_r($user->all()->execute()); // select all set columns print_r($user->select("name, email")->all()->execute()); // select all set limit print_r($user->all()->limit(1)->execute()); // select all set order by print_r($user->all()->orderBy("id DESC")->execute()); // select all return first print_r($user->all()->first()); // select by id and fill in the model $user->findById(2)->fill(); print_r($user); // select by id and return object stdClass with the data print_r($user->findById(2)->first()); // select by id and update $user->findById(1)->fill(); $user->email = "update@gmail.com"; $user->save(); // select by condition without parameters | not recommended $result = $user->findBy("email = 'update@gmail.com'")->execute(); print_r($result); // select by condition with parameters | recommended $result = $user->findBy("email = :email")->params([ "email" => "update@gmail.com" ])->execute(); print_r($result); // destroy user current $user->findById(4)->fill(); $user->destroy(); // delete by condition without parameters | not recommended $user->delete("email = 'test@gmail.com'"); // delete by condition with parameters | recommended $user->delete("email = :email", [ "email" => "test@gmail.com" ]); // mode of use select joins (INNER JOIN, LEFT JOIN, RIGHT JOIN...) $user->findById(9)->fill(); print_r($user->posts()); // erros print_r($user->error()); echo "<pre>"; ?>
basecode/querymodel 适用场景与选型建议
basecode/querymodel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「BaseCode」 「Thcoder」 「QueryModel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 basecode/querymodel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 basecode/querymodel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 basecode/querymodel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
marmot framework basecode
A package to easily make use of TypeId with PHP.
Component route, componente para criação de projetos mvc com rotas.
CurlRequest Component...
Base Laravel code to implement crud api
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-09