seguncodes/smyorm
Composer 安装命令:
composer create-project seguncodes/smyorm
包简介
SmyORM is a PHP Object-relational mapping (ORM) that allows developers to write code in simple programming languages of their choice instead of using SQL to access, add, update, and delete data and schemas in the respective database.
README 文档
README
DESCRIPTION
SmyORM is a PHP Object-relational mapping (ORM) that allows developers to write code in simple programming languages of their choice instead of using SQL to access, add, update, and delete data and schemas in the respective database. It is currently being used by the SmyPhp framework
REQUIREMENTS
- php 7.3^
- composer
INSTALLATION
$ composer require seguncodes/smyorm
USAGE
Instantiating the ORM
Create a Model for each database table eg User.php or Transaction.php; Or a basic file to handle the SQL operations for a specific table. Then instantiate the ORM this way:
User.php file
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } }
Query Builders
The ORM comes with various query builders
save()
The save method saves data into database
User.php file
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testSave() { // Create a sample object with data $data = [ 'name' => 'Joe', 'email' => 'john@doe.com', 'role' => 'User' ]; foreach ($data as $attribute => $value) { $this->orm->{$attribute} = $value; } // Call the save() method $result = $this->orm->save(); return $result; } }
findOne()
finds row WHERE argument exists and returns only 1
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testFindOne() { // Call the findOne() method $result = $this->orm->findOne([ "id" => 16, "name" => "John" ]); return $result; } }
findOneOrWhere()
This takes in two arguments with the second argument being the OR condition and returns only one result
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testfindOneOrWhere() { // Call the findOneOrWhere() method $result = $this->orm->findOneOrWhere([ "id" => 16 ], [ "role" => "Admin" ]); return $result; } }
findAll()
This performs the basic SELECT all functionality
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testFindAll() { // Call the findAll() method $result = $this->orm->findAll(); return $result; } }
findAllWhere()
This performs the findAll functionality with a WHERE clause
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testfindAllWhere() { // Call the findAllWhere() method $result = $this->orm->findAllWhere([ "id" => 1 ]); return $result; } }
findAllOrWhere()
This performs the findAll functionality with a WHERE clause with the second argument being the OR condition
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testfindAllOrWhere() { // Call the findAllOrWhere() method $result = $this->orm->findAllOrWhere([ "id" => 13 ], [ "name" => "joe" ]); return $result; } }
count()
This counts the number of columns in a table
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testCount() { // Call the count() method $result = $this->orm->count(); return $result; } }
countWhere()
This counts the number of columns with a WHERE clause
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testcountWhere() { // Call the countWhere() method $result = $this->orm->countWhere([ "id" => 1 ]); return $result; } }
countOrWhere()
This counts the number of columns with a WHERE clause and the second argument being the OR condition
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testcountOrWhere() { // Call the countOrWhere() method $result = $this->orm->countOrWhere([ "id" => 13 ], [ "id" => 15 ]); return $result; } }
delete()
This takes a WHERE clause and deletes a row or rows
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testdelete() { // Call the delete() method $result = $this->orm->delete([ "id" => 1 ]); return $result; } }
deleteOrWhere()
This takes a WHERE clause and the second argument being the OR condition then deletes corresponding row or rows
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testdeleteOrWhere() { // Call the countOrWhere() method $result = $this->orm->deleteOrWhere([ "id" => 13 ], [ "id" => 15 ]); return $result; } }
update()
This takes two arguments, the data to be updated and the WHERE clause
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testUpdate() { // Call the update() method $result = $this->orm->update([ "name" => "john Doe" ], [ "id" => 13 ]); return $result; } }
updateOrWhere()
This takes three arguments, the data to be updated, a WHERE clause and an OR condition
require_once __DIR__ . '/vendor/autoload.php'; use SmyORM\SmyORM\Orm; class User{ private $orm; public function __construct() { $this->setUp(); } public function setUp(): void { $db = new \PDO('mysql:host=localhost;dbname=yourDatabaseName', 'username', 'password'); // Create a concrete class that extends the ORM class $this->orm = new class($db) extends Orm { public function tableName(): string { return 'users'; // the name of the table that this model references } public function attributes(): array { return ['name', 'email', 'role']; // attributes of the table } }; } public function testupdateOrWhere() { // Call the updateOrWhere() method $result = $this->orm->updateOrWhere([ "name" => "Joe doe" ], [ "id" => 17, "role" => "User" ], [ "id" => 13, "role" => "Admin" ]); return $result; } }
Contributing & Vulnerabilities
If you would like to contribute or you discover a security vulnerability in the SmyORM, your pull requests are welcome. However, for major changes or ideas on how to improve the library, please create an issue.
License
The SmyORM is open-sourced software licensed under the MIT license.
seguncodes/smyorm 适用场景与选型建议
seguncodes/smyorm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 05 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php orm」 「smyphp」 「smyorm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 seguncodes/smyorm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 seguncodes/smyorm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 seguncodes/smyorm 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
PeskyORM - annoying ORM that contains tons of exceptions and throws them when anything goes wrong
An ORM solution for Salesforce Object Query Language (SOQL)
LDAP ORM for Symfony2
Alfabank REST API integration
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-29