andikaryanto11 / ci4orm
Codeigniter 4 的 ORM 数据库映射
Requires
- php: ^7.4
- fzaninotto/faker: 1.9.2
This package is auto-updated.
Last update: 2024-09-10 21:13:52 UTC
README
此包旨在将您的数据库映射到您创建的实体/类。灵感来源于.NET Entity Framework。
规则
创建类,然后扩展到这个 Eloquent
安装
composer require andikaryanto11/ci4orm
属性
-
protected $table = "your_table_name" 是必须设置的属性
-
static $primaryKey = "your_primary_key_field_name" 是必须设置的属性
use AndikAryanto11\Eloquent; class MyEloquent extends Eloquent{ public $FieldInTable; public $AnotherFieldInTable; protected $table = "MyTable"; static $primaryKey = "MyPKfield"; public function __construct() { $db = \Config\Database::connect(); parent::__construct($db); } }
-
protected $hideFieldValue = [],当您将字段放入此数组时,在从 eloquent 获取数据时将取消设置该字段
protected $hideFieldValue = [ 'Password' ]
-
protected $cast = [],当您将字段放入此数组时,在从 eloquent 获取数据时将将其转换为特定数据类型。可用的转换数据类型包括:- integer - boolean - decimal:2 -> 2 将是两位小数的十进制数字 - datetime:Y-m-d -> Y-m-d 将是格式化的日期 - string
protected $cast = [ 'Id' => 'integer', 'IsLoggedIn' => 'boolean', 'Paid' => 'decimal:2', 'Created' => 'datetime:Y-m-d', ];
方法
-
find($id)
将从您的表中获取 "id" 的数据
Entity::find(1); // return your Entity Object with "Id" = 1 or null
-
findOrNew($id)
将从您的表中获取 "id" 的数据或如果为 null,则获取新对象
Enitity::findOrNew(1); //return your Entity Object with "Id" = 1 or New object if no data found
-
findOrFail($id)
将从您的表中获取 "id" 的数据或如果为 null,则获取新对象
Enitity::findOrFail(1); //return your Entity Object with "Id" = 1 or throw an error
-
findOne(array $params)
将从您的表中的结果的第一行获取数据
$params = [ "where" => [ "colum_name" => "some_value" ] ]; Entity::findOne($params); //$params is nullable //return first row of results or null;
-
findOneOrNew(array $params)
将从您的表中的结果的第一行获取数据或新对象
$params = [ "where" => [ "colum_name" => "some_value" ] ]; Entity::findOneOrNew($params); //$params is nullable //return first row of results or new object;
-
findOneOrFail(array $params)
将从您的表中的结果的第一行获取数据或抛出错误
$params = [ "where" => [ "colum_name" => "some_value" ] ]; Entity::findOneOrFail($params); //$params is nullable //return first row of results or throw error;
-
findAll(array $params)
将从您的表中获取数据或 null
$params = [ "where" => [ "colum_name" => "some_value" ] ]; Entity::findAll($params); //$params is nullable //return first row of results or null;
-
findAllOrFail(array $params)
将从您的表中获取数据或抛出错误
$params = [ "where" => [ "colum_name" => "some_value" ] ]; Entity::findAll($params); //$params is nullable //return first row of results or throw error;
-
beforeSave()
将在 save 方法之前执行。如果您想在保存之前做些什么,则覆盖此方法。
-
save()
将存储您的数据到表中或更新您的数据。如果您的 "primary_key" 实体为 null,则将保存数据,否则将更新;
$ent = new Entity(); $ent->Name = "whatever"; $ent->save(); //insert $ent = Entity::find(1); $ent->Name = "whatever"; $ent->save(); //update
-
hasOne(string $relatedEloquent, string $foreignKey)
将获取您的相关表数据父或 null
$ent = Entity::find(1); $parent = $ent->hasOne("Your\EntityNamespace\EntityName", "$ent foregin_key_name"); // $parent is data parent of your related table;
-
hasOneOrNew(string $relatedEloquent, string $foreignKey)
将获取您的相关表数据父或新对象
$ent = Entity::find(1); $parent = $ent->hasOneOrNew("Your\EntityNamespace\EntityName", "$ent foregin_key_name"); // $parent is data parent of your related table or new object;
-
hasOneOrFail(string $relatedEloquent, string $foreignKey)
将获取您的相关表数据父或抛出错误
$ent = Entity::find(1); $parent = $ent->hasOneOrNew("Your\EntityNamespace\EntityName", "$ent foregin_key_name"); // $parent is data parent of your related table or new object;
-
hasMany(string $relatedEloquent, string $foreignKey, array $params)
将获取您的相关表数据子或 null
$ent = Entity::find(1); $params = [ "where" => [ "colum_name" => "some_value" ] ]; $child = $ent->hasMany("Your\EntityNamespace\EntityName", "$ent foregin_key_name", $params); //$params is nullable // $child is data child of your related table or new object;
-
hasManyOrFail(string $relatedEloquent, string $foreignKey, array $params)
将获取您的相关表数据子或抛出错误
$ent = Entity::find(1); $params = [ "where" => [ "colum_name" => "some_value" ] ]; $child = $ent->hasMany("Your\EntityNamespace\EntityName", "$ent foregin_key_name", $params); //$params is nullable // $child is data parent of your related table or new object;
-
hasFirst(string $relatedEloquent, string $foreignKey, $params = []) 将获取第一条数据
$ent = Entity::find(1); $params = [ "where" => [ "colum_name" => "some_value" ] ]; $child = $ent->hasFirst("Your\EntityNamespace\EntityName", "$ent foregin_key_name", $params); //$params is nullable // $child is data parent of your related table or new object;
-
collect(array $filter = []) 将返回 EloquentList,在 vendor 文件夹中打开以查看可用方法。
$obj = Entity::collect()
-
paging($filter = [], $page = 1, $size = 6, $showedPage = 5, $queryParams = []) 将返回数据数组和其他属性
$obj = Entity::paging([], 1, 6, 5, []);
-
datatables(array $filter = [], boolean $returnEntity = true, boolean $useIndex = true) 将返回 EloquentDatatables,在 vendor 文件夹中打开以查看可用方法。
$obj = Entity::datatables([], true, true);
参数 ($params)
通用 $params 用于过滤数据
$params = [
"join" => [
"table_name" => [[
"key" => "table_name.key = table_name.key",
"type" => "LEFT" || "RIGHT" //optional
]]
],
"you can add more key params below, 'where', 'whereIn', etc"
];
$params = [
"where" => [
"colum_name" => "some_value"
]
];
$params = [
"orWhere" => [
"colum_name" => "some_value"
]
];
$params = [
"whereIn" => [
"colum_name" => ["some_value", "other_value"]
]
];
$params = [
"orWhereIn" => [
"colum_name" => ["some_value", "other_value"]
]
];
$params = [
"whereNotIn" => [
"colum_name" => ["some_value", "other_value"]
]
];
$params = [
"like" => [
"colum_name" => "some_value"
]
];
$params = [
"orLike" => [
"colum_name" => "some_value"
]
];
$params = [
"orLike" => [
"colum_name" => "some_value"
]
];
$params = [
"order" => [
"colum_name" => "ASC",
"colum_name" => "DESC"
]
];
$params = [
"limit" => [
"page" => "ASC",
"size" => "DESC"
]
];