osyra / syra
0.4.5
2024-05-16 09:19 UTC
Requires
- php: >=8.0.0
README
Syra 是一个 PHP 对象关系映射库。
安装
您可以通过 composer 安装它:osyra/syra
定义所需的类
您需要创建两个类。
namespace App; class CustomDatabase implements \Syra\DatabaseInterface { private static $writer, $reader; public static function getWriter() { if(is_null(self::$writer)) { self::$writer = new \Syra\MySQL\Database('hostname', 'user', 'password'); self::$writer->connect(); } return self::$writer; } public static function getReader() { if(is_null(self::$reader)) { // if you have only one server self::$reader = self::getWriter(); // if you have a slave for read only you could write this : // self::$reader = new \Syra\MySQL\Database('ro-hostname', 'ro-user', 'ro-password'); // self::$reader->connect(); } return self::$reader; } }
namespace App; class CustomRequest extends \Syra\MySQL\Request { const DATABASE_CLASS = '\\App\\CustomDatabase'; protected function buildClassFromTable($table) { return '\\App\\Model\\'.$table; // this must return the name of the class matched by the table } }
然后对于您的数据库中的每个表,您将添加一个类。表的键必须是 id
,它可以是整数或字符串。
namespace App\Model; class Foobar extends \Syra\MySQL\ModelObject { const DATABASE_CLASS = '\\App\\CustomDatabase', DATABASE_SCHEMA = 'Schema', DATABASE_TABLE = 'Foobar'; protected static $properties = [ 'id' => ['class' => 'Integer'], 'name' => ['class' => 'String'], 'parent' => ['class' => '\\App\\Model\\Bar'] ]; protected $id, $name, $parent; } class Bar extends \Syra\MySQL\ModelObject { const DATABASE_CLASS = '\\App\\CustomDatabase', DATABASE_SCHEMA = 'Schema', DATABASE_TABLE = 'Bar'; protected static $properties = [ 'id' => ['class' => 'Integer'], 'name' => ['class' => 'String'], 'createdAt' => ['class' => 'DateTime'] ]; protected $id, $name, $createdAt; }
请求数据
请求数据对象
$foobars = \App\CustomRequest::get('Foobar')->withFields('id', 'name') ->mapAsObjects(); $foobar = \App\CustomRequest::get('Foobar')->withFields('id', 'name') ->where('', 'Foobar', 'id', '=', 1) ->mapAsObject();
带条件的请求数据对象
条件是任何 where() 函数的第一个参数。它可以以一个闭括号开头,后跟 AND 或 OR,并以一个开括号结尾。
$foobars = \App\CustomRequest::get('Foobar')->withFields('id', 'name') ->where('', 'Foobar', 'parent', '=', $parentID) ->where('AND (', 'Foobar', 'name', 'LIKE', '%Hello%') ->where('OR', 'Foobar', 'name', 'LIKE', '%World') ->mapAsObjects();
排序
$foobars = \App\CustomRequest::get('Foobar')->withFields('id', 'name') ->orderAscBy('Foobar', 'name') ->orderDescBy('Foobar', 'id') ->mapAsObjects();
限制和偏移量
这将从数据库的第10行开始获取10行。
$foobars = \App\CustomRequest::get('Foobar')->withFields('id', 'name') ->lines(10) ->offset(10) ->mapAsObjects();
注意:请注意,这是行数的限制,而不是对象的数量。
链接表以获取子对象
$foobars = \App\CustomRequest::get('Foobar')->withFields('id', 'name') ->leftJoin('Bar')->on('Foobar', 'parent')->withFields('id', 'name', 'createdAt') ->mapAsObjects();
链接表以获取集合
$bars = \App\CustomRequest::get('Bar')->withFields('id', 'name', 'createdAt') ->leftJoin('Foobar', 'Foobars')->on('Bar', 'id', 'parent')->withFields('id', 'name') ->mapAsObjects(); foreach($bars as $bar) { foreach($bar->myFoobars as $foobar) { ... } }
链接表的条件
$bars = \App\CustomRequest::get('Bar')->withFields('id', 'name', 'createdAt') ->leftJoin('Foobar', 'Foobars')->on('Bar', 'id', 'parent')->withFields('id', 'name')->with('', 'name', 'LIKE', '%Hello%') ->mapAsObjects();
在相同请求的两个表的字段之间添加额外的条件
$reference = new \stdClass; $reference->table = 'Bar'; $reference->field = 'language'; $foobars = \App\CustomRequest::get('Foo')->withFields('id', 'language', 'name') ->leftJoin('Bar', 'Bars')->on('Foo', 'id', 'foo')->with('', 'language', '=', $reference)->withFields('id', 'foo', 'language') ->mapAsObject();
多次使用同一张表
$bars = \App\CustomRequest::get('Bar')->withFields('id', 'name') ->leftJoin('Bar')->on('Bar', 'parent')->withFields('id', 'name') # equivalent to ->on('Bar::1', 'parent') ->leftJoin('Bar')->on('Bar::2', 'parent')->withFields('id', 'name') ->mapAsObjects();
以关联数组的形式请求数据(用于后续的 JSON 编码)
$foobars = \App\CustomRequest::get('Foobar')->withFields('id', 'name') ->mapAsArrays();
将对象转换为数组
$foobars = \App\CustomRequest::get('Foobar')->withFields('id', 'name') ->mapAsObjects(); $arrays = \App\CustomRequest::objectsAsArrays($foobars); $foobar = \App\CustomRequest::get('Foobar')->withFields('id', 'name') ->where('', 'Foobar', 'id', '=', 1) ->mapAsObject(); $array = $foobar->asArray();
修改对象
保存
$foobar = \App\CustomRequest::get('Foobar')->withFields('id', 'name') ->where('', 'Foobar', 'id', '=', 1) ->mapAsObject(); $foobar->name = 'Something'; $foobar->save(); \App\Database::getWriter()->commit();
删除
$foobar = \App\CustomRequest::get('Foobar')->withFields('id', 'name') ->where('', 'Foobar', 'id', '=', 1) ->mapAsObject(); $foobar->delete(); \App\Database::getWriter()->commit();