seguncodes / smyorm
SmyORM 是一个 PHP 对象关系映射(ORM)框架,允许开发者使用他们选择的简单编程语言编写代码,而不是使用 SQL 来访问、添加、更新和删除数据库中的数据以及模式。
Requires (Dev)
- phpunit/phpunit: 10.2.x-dev
This package is auto-updated.
Last update: 2024-09-29 19:04:30 UTC
README
DESCRIPTION
SmyORM 是一个 PHP 对象关系映射(ORM)框架,允许开发者使用他们选择的简单编程语言编写代码,而不是使用 SQL 来访问、添加、更新和删除数据库中的数据以及模式。目前它被 SmyPhp 框架使用。
REQUIREMENTS
- php 7.3^
- composer
INSTALLATION
$ composer require seguncodes/smyorm
USAGE
实例化 ORM
为每个数据库表创建一个模型,例如 User.php 或 Transaction.php;或者创建一个基本的文件来处理特定表的 SQL 操作。然后以这种方式实例化 ORM:
User.php
文件
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 } }; } }
查询构建器
ORM 内置了各种查询构建器 save()
保存方法将数据保存到数据库中
User.php
文件
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()
根据存在的参数找到行并返回一行
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()
接受两个参数,第二个参数是 OR 条件,并返回一个结果
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()
执行基本的 SELECT ALL 功能
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()
在 WHERE 子句下执行 findAll 功能
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()
在 WHERE 子句和 OR 条件下执行 findAll 功能
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()
计算表中列的数量
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()
在 WHERE 子句下计算列的数量
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()
在 WHERE 子句和 OR 条件下计算列的数量
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()
根据 WHERE 子句删除一行或多行
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()
根据 WHERE 子句和 OR 条件删除相应的行或行
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()
接受两个参数,要更新的数据和 WHERE 子句
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()
接受三个参数,要更新的数据、WHERE 子句和 OR 条件
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; } }
贡献 & 漏洞
如果您想贡献或发现 SmyORM 中的安全漏洞,您的拉取请求欢迎。然而,对于主要更改或改进库的想法,请创建一个问题。
许可证
SmyORM 是开源软件,许可协议为 MIT 许可证。