吉吉霍霍科科 / ichi-orm
PHP ORM
Requires
- php: >=7.3|>=8.0
README
Ichi ORM 的目标是成为具有简单使用方式的快速性能和安全的 PHP 数据库 ORM。
许可证
此软件包根据 MIT 许可证 是开源的
目录
安装
composer require jijihohococo/ichi-orm
设置数据库连接
此库可以连接 MySQL、Postgres 和 MS SQL Server。
首先,您需要像下面这样声明您的数据库驱动程序。
use JiJiHoHoCoCo\IchiORM\Database\Connector; $connector=new Connector; $connector->createConnection('mysql',[ 'dbname' => 'database_name', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'host' => '127.0.0.1', 'user_name' => 'user_name', 'user_password' => 'user_password' ]);
如果您想添加另一个自定义数据库连接,您可以这样做。
您必须在数据库连接中添加 dbname、host、user_name 和 user_password。我建议您使用 "utf8mb4" 作为数据库字符集,并使用 "utf8mb4_unicode_ci" 作为数据库校对。
在默认数据库连接中,您不需要添加驱动程序参数,但在自定义数据库连接中,您必须添加驱动程序参数。
$connector->addConnection('new_mysql_connection')->createConnection('new_mysql_connection',[ 'driver' => 'mysql', 'dbname' => 'database_name', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'host' => '127.0.0.1', 'user_name' => 'user_name', 'user_password' => 'user_password' ]);
默认数据库连接是 'mysql'、'pgsql' 和 'sqlsrv'。
支持的数据库驱动程序是 'mysql'、'pgsql' 和 'sqlsrv'。
在声明数据库连接后,您可以选择默认数据库连接
$connector->selectConnection('mysql');
可用的数据库设置
表结构
如果您有名为 "deleted_at" 的列,请确保该列是可空的列。
从命令行创建模型
首先,您需要在您的项目文件夹下创建名为 "ichi" 的文件,并在该文件中使用以下代码
#!/usr/bin/env php <?php require __DIR__.'/vendor/autoload.php'; use JiJiHoHoCoCo\IchiORM\Command\ModelCommand; $modelCommand=new ModelCommand; $modelCommand->run(__DIR__,$argv);
然后您可以在您的命令行中创建模型
php ichi make:model Blog
默认的文件文件夹是 "app/Models"。因此,在执行命令后,您创建的模型将位于此默认文件文件夹中。如果您想更改默认文件夹路径,您可以在 "ichi" 文件中更改它。
$modelCommand=new ModelCommand; $modelCommand->setPath('new_app/Models'); $modelCommand->run(__DIR__,$argv);
配置表名
在 Ichi ORM 中,一个扩展 "JiJiHoHoCoCo\IchiORM\Database\Model" 抽象类的模型类表示一个表。
默认情况下,模型类的表名将按照以下格式显示
如果上述格式不适合模型类,您可以在模型类中进行自定义
namespace App\Models; use JiJiHoHoCoCo\IchiORM\Database\Model; class Blog extends Model{ protected function getTable(){ return "order_item_details"; } }
配置主键
默认情况下,表的键是 "id"。如果您想更改它,您可以在模型类中进行自定义
namespace App\Models; use JiJiHoHoCoCo\IchiORM\Database\Model; class Blog extends Model{ protected function getID(){ return "blog_id"; } }
CRUD
首先,您需要从您的类扩展 Model 类,并在您的模型中将数据字段作为属性声明,如下所示。
namespace App\Models; use JiJiHoHoCoCo\IchiORM\Database\Model; class Blog extends Model{ publilc $id,$author_id,$content,$created_at,$updated_at,$deleted_at; }
创建
您可以创建如下所示的数据。
Blog::create([ 'author_id' => 1, 'content' => 'Content' ]);
您可以选择是否在 "create" 函数中将可空字段数据添加到数组中。
如果您有 "created_at" 数据字段,您不需要为该数据字段添加任何数据。Ichi ORM 将自动插入当前日期和时间。数据字段必须是时间戳或 varchar 格式。
创建后,您可以获取新的模型对象。
App\Models\Blog 对象 ( [id] => 1 [author_id] => 1 [content] => 内容 [created_at] => 2021-10-01 12:02:26 [updated_at] => [deleted_at] => )
禁用自动增加 ID
如果你的表中没有使用自增ID,你必须在模型类中编写此函数。
protected function autoIncrementId(){ return FALSE; }
你必须手动添加你的ID值,如下所示。
Blog::create([ 'id' => 1 , 'author_id' => 1, 'content' => 'Content' ]);
在一个查询中插入多行
如果你想在一个查询中插入多行,你可以按照以下编码流程进行。
use App\Models\Blog; $contents=$_REQUEST['content']; $insertBlogs=[]; foreach ($contents as $key => $content) { $insertBlogs[]=[ 'content' => $content, 'author_id' => $_REQUEST['author_id'][$key] ]; } Blog::insert($insertBlogs);
检索
你可以通过以下方式根据主键获取你的数据。
Blog::find(1);
如果你不想通过主键获取你的数据,你可以按照以下方式操作。
Blog::findBy('content','Content');
第一个参数是字段名,第二个参数是值。
你可以通过使用 "find" 和 "findBy" 函数来获取单个对象。
参照
如果你数据库中有一对一关系(带有外键或没有外键),你可以在子模型类中使用 "refersTo" 函数,如下所示。该函数将输出单个对象。
如果你父模型的主键是 "id",你必须将父模型名称和表示父ID的字段添加到 "refersTo" 函数中。
namespace App\Models; use JiJiHoHoCoCo\IchiORM\Database\Model; class Blog extends Model{ publilc $id,$author_id,$content,$created_at,$updated_at,$deleted_at; public function author(){ return $this->refersTo('App\Models\Author','author_id'); } }
如果你父模型的主键不是 "id",你必须将父模型名称、表示父ID的字段和父主键字段添加到 "refersTo" 函数中。
namespace App\Models; use JiJiHoHoCoCo\IchiORM\Database\Model; class Blog extends Model{ publilc $id,$author_id,$content,$created_at,$updated_at,$deleted_at; public function author(){ return $this->refersTo('App\Models\Author','author_id','authorID'); } }
你可以在控制器或类中作为单个对象获取父数据。
use App\Models\Blog; $blogObject=Blog::find(1); $authorObject=$blogObject->author(); $authorId=$authorObject->id;
你不需要担心空值。它具有空安全。
多参照
如果你数据库中有一对多关系(带有外键或没有外键),你可以在父模型类中使用 "refersMany" 函数,如下所示。该函数将输出对象数组。
如果你父模型的主键是 "id",你必须将子模型名称和表示父ID的字段添加到 "refersMany" 函数中。
namespace App\Models; use JiJiHoHoCoCo\IchiORM\Database\Model; class Author extends Model{ publilc $id,$name,$created_at,$updated_at,$deleted_at; public function blogs(){ return $this->refersMany('App\Models\Blog','author_id')->get(); } }
如果你父模型的主键不是 "id",你必须将子模型名称、表示父ID的字段和父主键字段添加到 "refersMany" 函数中。
namespace App\Models; use JiJiHoHoCoCo\IchiORM\Database\Model; class Author extends Model{ publilc $authorID,$name,$created_at,$updated_at,$deleted_at; public function blogs(){ return $this->refersMany('App\Models\Blog','author_id','authorID')->get(); } }
你可以自定义子查询。
return $this->refersMany('App\Models\Blog','author_id','authorID')->latest()->get();
你可以在控制器或类中作为对象数组获取子数据。
use App\Models\Author; $authorObject=Author::find(1); $blogs=$authorObject->blogs();
更新
你可以按照以下方式更新你的数据。
Blog::find(1)->update([ 'content' => 'New Content' ]);
更新后,你可以获取模型对象。
如果你有 "updated_at" 数据字段,你不需要为此数据字段添加任何数据。Ichi ORM 将自动为此数据字段插入当前日期时间。该数据字段必须是时间戳或 varchar 格式。
App\Models\Blog 对象 ( [id] => 1 [author_id] => 1 [content] => 新内容 [created_at] => 2021-10-01 12:02:26 [updated_at] => 2021-10-01 12:03:26 [deleted_at] => )
在一个查询中更新多行
如果你想在一个查询中更新多行,你可以按照以下编码流程进行。
use App\Models\Blog; $blogs=Blog::get(); $updateBlogs=[]; foreach($blogs as $key => $blog){ $updateBlogs[]=[ 'content' => $_REQUEST['content'][$key], 'author_id' => $_REQUEST['author_id'][$key], ]; } Blog::bulkUpdate($updateBlogs);
删除
你可以按照以下方式删除你的数据。
Blog::find(1)->delete();
如果你有 "deleted_at" 数据字段且 "deleted_at" 数据字段是可空的,你有软删除功能。因此,删除数据后,这些数据实际上不会被删除,但在默认查询中不会显示。
如果没有 "delete_at" 数据字段,则不能使用软删除功能,数据将被删除。
如果你想恢复你的软删除数据,你可以按照之前的方式操作。
Blog::find(1)->restore();
如果你想强制删除你的数据(无论它是否可以软删除),你可以按照之前的方式操作。
Blog::find(1)->forceDelete();
查询
SELECT
要执行 "SELECT" SQL 查询,你可以使用 "select" 函数,如下所示。
Blog::select(['id'])
Blog::select(['blogs.id'])
Blog::select(['id','content'])
Blog::select(['blogs.id','blogs.content'])
获取查询数据
你可以使用 "get()" 和 "toArray()" 函数获取你的查询数据。
获取
"get()" 函数可以在主查询和子查询中使用。当它在主查询中使用时,如以下所示,它将返回相关模型的对象数组。
Array ( [0] => App\Models\Blog 对象 ( [id] => 1 [author_id] => 1 [content] => 内容 [created_at] => 2021-10-01 12:02:26 [updated_at] => 2021-10-01 12:02:26 [deleted_at] => ) )
因为 "get()" 函数输出对象数组,所以你可以直接在循环中使用对象调用关系函数。
$blogs=Blog::select(['id','content'])->get(); foreach($blogs as $blog){ echo $blog->id . '<br>'; echo $blog->author()->name . '<br>'; }
如果您不使用选择函数,您将获得相关模型的所有数据字段。
Blog::get();
转换为数组
"toArray()" 函数只能用在主查询中。此函数将返回查询数组,如下所示。
数组 ([0] => 数组 ([id] => 1 [author_id] => 1 [content] => 内容 [created_at] => 2021-10-01 12:02:26 [updated_at] => 2021-10-01 12:02:26 [deleted_at] => ) )
由于 "toArray()" 函数输出数组,因此您不能直接在循环中的对象上调用关系函数。
您不能在子查询中使用 "toArray" 函数。
$blogs=Blog::select(['id','content'])->toArray(); foreach($blogs as $blog){ echo $blog['id'] . '<br>'; }
如果您不使用选择函数,您将获得相关模型的所有数据字段。
Blog::toArray();
获取包含软删除数据的查询数据
如果您有软删除的数据行,您将看不到这些数据在您的数组或数据对象数组中。如果您想看到包含软删除数据行的数组或数据对象数组,您必须使用如下所示的 "withTrashed()" 函数。
Blog::withTrashed()->select(['id','content'])->get(); Blog::withTrashed()->select(['id','content'])->toArray();
如果您不使用选择函数,您将获得相关模型的所有数据字段。如果您使用 "withTrashed()" 函数,您还将获得软删除的数据行。
Blog::withTrashed()->get(); Blog::withTrashed()->toArray();
LIMIT
要制作限制 SQL 查询,您可以使用 "limit" 函数,并将整数放入此函数中,如下所示
在主查询中
Blog::limit(1)->get(); Blog::limit(1)->toArray();
在子查询中
Blog::whereIn('id',function($query){ return $query->select(['id'])->limit(1)->get(); })->get(); Blog::whereIn('id',function($query){ return $query->select(['id'])->limit(1)->get(); })->toArray();
WHERE
要制作 "WHERE" SQL 查询,您可以使用 "where" 函数,如下所示
在 "=" 的情况下
Blog::where('id',1)->get();
如果您想添加运算符
Blog::where('id','=',1)->get(); Blog::where('content','like','%Content%')->get();
OR WHERE
要制作 "OR WHERE" SQL 查询,您可以使用 "orWhere" 函数,如下所示
在 "=" 的情况下
Blog::where('id',1)->orWhere('content','Content')->get();
如果您想添加运算符
Blog::where('id',1)->orWhere('content','=','Content')->get(); Blog::where('id',1)->orWhere('content','like','%Content%')->get();
WHERE IN
要制作 "WHERE IN" SQL 查询,您可以使用 "whereIn" 函数,如下所示
Blog::whereIn('id',[1,2])->get();
WHERE NOT IN
要制作 "WHERE NOT IN" SQL 查询,您可以使用 "whereNotIn" 函数,如下所示
Blog::whereNotIn('id',[1,2])->get();
连接
规则和流程与 SQL Join 相同。
内连接
单个 SQL 查询
Author::innerJoin('blogs','authors.id','=','blogs.author_id') ->select(['authors.*','blogs.id AS blog_id']) ->get();
子查询
Blog::where('id',function($query){ return $query->from('App\Models\Author') ->innerJoin('blogs','authors.id','=','blogs.author_id') ->select(['blogs.id AS blog_id']) ->get(); })->get();
左连接
单个 SQL 查询
Author::leftJoin('blogs','authors.id','=','blogs.author_id') ->select(['authors.*','blogs.id AS blog_id']) ->get();
子查询
Blog::where('id',function($query){ return $query->from('App\Models\Author') ->leftJoin('blogs','authors.id','=','blogs.author_id') ->select(['blogs.id AS blog_id']) ->get(); })->get();
右连接
单个 SQL 查询
Author::rightJoin('blogs','authors.id','=','blogs.author_id') ->select(['authors.*','blogs.id AS blog_id']) ->get();
子查询
Blog::where('id',function($query){ return $query->from('App\Models\Author') ->rightJoin('blogs','authors.id','=','blogs.author_id') ->select(['blogs.id AS blog_id']) ->get(); })->get();
并集
您可以在查询中使用 "union" 函数。
Blog::where('id',1)->union(function(){ return Blog::where('id',2)->toSQL()->get(); })->get();
您可以在子查询中使用 "union" 函数。
Blog::whereIn('id', function($query) { return $query->select(['id'])->where('id',1)->union(function($query){ return $query->select(['id'])->where('id',2)->get(); })->get(); } )->get();
分页
在这个库中,您可以使用两种类型的分页。
- 数据库分页
- 数组分页
默认每页分页数据为 10。您可以自定义这个数字。分页函数将根据以下格式输出数组。因此,您可以使用该数组数据将服务器分页集成到您的前端(如 Vue 和 React)中。
[ 'current_page' => 'current page number', 'data' => 'paginated data', 'first_page_url' => 'first page url', 'from' => 'The number of paginated data which starts to show in current page', 'last_page' => 'The last page number', 'last_page_url' => 'The last page url', 'next_page_url' => 'The next page url', 'path' => 'the current page url', 'per_page' => 'The number of how many data will be shown per page', 'prev_page_url' => 'The previous page url', 'to' => 'The number of paginated data which is last data to show in current page', 'total' => 'The total number of paginated data in current page' ]
数据库分页
您可以这样分页查询结果
$paginatedBlogs=Blog::whereIn('id',[1,2,3,4,5])->paginate();
您可以通过以下方式自定义分页数据数量
$paginatedBlogs=Blog::whereIn('id',[1,2,3,4,5])->paginate(12);
您可以通过以下方式获取分页数据。在 "data" 数组键中的数据是对象数组。
foreach($paginatedBlogs['data'] as $blog){ echo $blog->id.'<br>'; echo $blog->author()->name . '<br>'; }
您可以直接在循环中的对象上调用关系函数。
您可以在您的前端 PHP 文件中使用分页用户界面
(new JiJiHoHoCoCo\IchiORM\UI\Pagination)->paginate($paginatedBlogs);
您可以自定义分页用户界面的颜色
(new JiJiHoHoCoCo\IchiORM\UI\Pagination)->paginate($paginatedBlogs,'#000000');
数组分页
您可以通过以下方式分页数组。
use JiJiHoHoCoCo\IchiORM\Pagination\ArrayPagination; $blogs=['Blog One','Blog Two','Blog Three','Blog Four','Blog Five']; $paginatedBlogs=(new ArrayPagination)->paginate($blogs);
您还可以使用多维数组
use JiJiHoHoCoCo\IchiORM\Pagination\ArrayPagination; $blogs=[ [ 'content' => 'Blog One', 'author_name' => 'John Doe' ], [ 'content' => 'Blog Two', 'author_name' => 'Joe Blow' ], [ 'content' => 'Blog Three', 'author_name' => 'Everyman' ], [ 'content' => 'Blog Four', 'author_name' => 'John Doe' ], [ 'content' => 'Blog Five', 'author_name' => 'John Doe' ] ]; $paginatedBlogs=(new ArrayPagination)->paginate($blogs);
您可以通过以下方式自定义分页数据数量
$paginatedBlogs=(new ArrayPagination)->paginate($blogs,2);
您可以在您的前端 PHP 文件中使用分页用户界面
(new JiJiHoHoCoCo\IchiORM\UI\Pagination)->paginate($paginatedBlogs);
您可以自定义分页用户界面的颜色
(new JiJiHoHoCoCo\IchiORM\UI\Pagination)->paginate($paginatedBlogs,'#000000');
子查询
如果您想在单个表中使用子查询,可以像之前那样做。
您可以在 "where"、"orWhere" 和 "whereIn" 函数中像以下那样使用子查询。
Blog::whereIn('author_id',function($query){ return $query->select(['id'])->where('id',1)->get(); })->get();
如果您想从不同表中使用子查询,可以像之前那样做。
Blog::whereIn('author_id',function($query){ return $query->from('App\Models\Author') ->select(['id']) ->where('id',1) ->get(); })->get();
您只能在子查询中使用 "from" 函数。您需要在 "from" 函数中添加代表另一个表的模型类名。
如果您想在选择中使用子查询,可以使用 "addSelect" 和 "addOnlySelect" 函数。
"addSelect" 函数是在选择查询中制作子查询。它将选择其函数内的数据与 "select" 函数的数据。如果您不使用 "select" 函数,它将选择其函数内的数据与所选表所有字段值的全部数据。
Blog::select(['id','author_id']) ->addSelect(['autor_name' => function($query){ return $query->from(['App\Models\Author']) ->whereColumn('authors.id','blogs.author_id') ->limit(1) ->get(); }])->get();
您不能在子查询中使用 "addSelect" 函数
"addOnlySelect" 函数是在选择查询中制作子查询。它将仅选择其函数内的数据。如果您想使用 "addOnlySelect" 函数,则不能使用其他选择函数("select" 和 "addSelect")。
Blog::addOnlySelect(['autor_name' => function($query){ return $query->from(['App\Models\Author']) ->whereColumn('authors.id','blogs.author_id') ->limit(1) ->get(); }])->get();
您可以在子查询中使用 "addOnlySelect" 函数
使用 PDO 函数
您可以使用像PDO函数这样的功能。您可以根据https://php.ac.cn/manual/en/class.pdo.php使用所有PDO函数。
如果您想使用PDO对象默认的数据库连接
$pdo=connectPDO();
如果您想使用PDO对象选择数据库连接
use JiJiHoHoCoCo\IchiORM\Database\Connector; $pdo=Connector::getInstance()->executeConnect('new_mysql_connection');
使用不同数据库
如果您有一个来自不同数据库的模型,您可以这样连接
namespace App\Models; use JiJiHoHoCoCo\IchiORM\Database\Model; use JiJiHoHoCoCo\IchiORM\Database\Connector; class Author extends Model{ protected function connectDatabase(){ return Connector::getInstance()->executeConnect('new_mysql_connection'); } }
JSON 响应
当您想为API创建JSON数据时,您可以像下面那样简单地进行。
return jsonResponse([ 'blogs' => Blog::get() ]);
您可以为JSON响应自定义HTTP响应代码。默认HTTP响应代码是200。
return jsonResponse([ 'blogs' => Blog::get() ],202);
如果您想自定义JSON数据,首先您需要创建类。
您必须扩展“JiJiHoHoCoCo\IchiORM\Resource\ResourceCollection”抽象类,并在您的所有资源集合类中声明“getSelectedResource()”函数。
namespace App\Resources; use JiJiHoHoCoCo\IchiORM\Resource\ResourceCollection; class BlogResourceCollection extends ResourceCollection{ public function getSelectedResource($data){ return [ 'id' => $data->id, 'author_id' => $data->author_id, 'content' => $data->content, 'created_at' => $data->created_at, 'updated_at' => $data->updated_at ]; } }
您可以在创建“ichi”文件后通过终端创建资源类,正如我们在从命令行创建模型中提到的。
php ichi make:resource BlogResourceCollection
观察者的默认路径是“app/Resources”。您也可以在“ichi”文件中更改此路径。
$modelCommand=new ModelCommand; $modelCommand->setResourcePath('new_app/Resources'); $modelCommand->run(__DIR__,$argv);
然后,您可以像下面那样显示您自定义的JSON资源。
对于对象数组-
return jsonResponse([ 'blogs' => (new BlogResourceCollection)->collection( Blog::get() ) ]);
对于单个对象-
return jsonResponse([ 'blog' => (new BlogResourceCollection)->singleCollection( Blog::find(1) ) ]);
您可以在资源集合类中声明您的关联(对于引用和引用多个)。
namespace App\Resources; use JiJiHoHoCoCo\IchiORM\Resource\ResourceCollection; class BlogResourceCollection extends ResourceCollection{ public function getSelectedResource($data){ return [ 'id' => $data->id, 'author' => $data->author(), 'content' => $data->content, 'created_at' => $data->created_at, 'updated_at' => $data->updated_at ]; } }
您可以在资源集合类中声明另一个资源集合(根据数据是单个对象还是对象数组)。
namespace App\Resources; use JiJiHoHoCoCo\IchiORM\Resource\ResourceCollection; use App\Resources\AuthorResourceCollection; class BlogResourceCollection extends ResourceCollection{ public function getSelectedResource($data){ return [ 'id' => $data->id, 'author_id' => $data->author_id, 'author' => (new AuthorResourceCollection)->singleCollection( $data->author() ) , 'content' => $data->content, 'created_at' => $data->created_at, 'updated_at' => $data->updated_at ]; } }
namespace App\Resources\AuthorResourceCollection; use JiJiHoHoCoCo\IchiORM\Resource\ResourceCollection; class AuthorResourceCollection extends ResourceCollection{ public function getSelectedResource($data){ return [ 'id' => $data->id, 'name' => $data->name ]; } }
缓存
您可以使用此库中的redis或memcached扩展缓存查询数据。
首先,您需要将redis或memcached对象传递给“JiJiHoHoCoCo\IchiORM\Cache\CacheModel”静态函数“setCacheObject”,如下所示。
使用Redis
use JiJiHoHoCoCo\IchiORM\Cache\CacheModel; use Redis; $redis = new Redis(); $redis->connect('127.0.0.1', 6379); CacheModel::setCacheObject($redis);
使用Memcached
use JiJiHoHoCoCo\IchiORM\Cache\CacheModel; use Memcached; $memcached = new Memcached(); $memcached->addServer('127.0.0.1',11211); CacheModel::setCacheObject($memcached);
根据安全性和端口可用性,连接redis或memcached的方式可能不同。重要的是您必须将redis或memcached对象传递给“JiJiHoHoCoCo\IchiORM\Cache\CacheModel”的“setCacheObject”静态函数。
然后,您可以调用缓存函数进行存储和检索。
use JiJiHoHoCoCo\IchiORM\Cache\CacheModel; use App\Models\Blog; $blogs=CacheModel::remember('blogs',function(){ return Blog::whereIn('author_id',[1,2,3])->get(); },100);
在“remember”函数中,您必须声明缓存的键名、存储的查询或数据以及以秒为单位的有效期。不添加有效期也可以,但数据将无限期保存。此函数将在声明的缓存键不在缓存服务器中时存储数据,如果在缓存服务器中,则检索缓存数据。
默认存储时间是无限制的。所以您必须为您的缓存服务器声明存储时间
如果您想删除您的缓存键,您可以这样做
use JiJiHoHoCoCo\IchiORM\Cache\CacheModel; CacheModel::remove('blogs');
您只需在缓存中保存您的数据
use JiJiHoHoCoCo\IchiORM\Cache\CacheModel; $blogs=CacheModel::save('blogs',function(){ return Blog::whereIn('author_id',[1,2,3])->get(); },100);
检索您的缓存数据
use JiJiHoHoCoCo\IchiORM\Cache\CacheModel; $cachedBlogs=CacheModel::get('blogs');
您可以获取回redis对象以实现redis扩展的功能。
use JiJiHoHoCoCo\IchiORM\Cache\CacheModel; $redisObject=CacheModel::getRedis();
您也可以获取回memcached对象以实现memcached的功能。
use JiJiHoHoCoCo\IchiORM\Cache\CacheModel; $memcachedObject=CacheModel::getMemcached();
观察者
要创建观察者,首先您需要创建实现"JiJiHoHoCoCo\IchiORM\Observer\ModelObserver"接口的观察者类。
在这个创建的类中,您必须声明以下函数。
namespace App\Observers; use JiJiHoHoCoCo\IchiORM\Observer\ModelObserver; use App\Models\Blog; class BlogObserver implements ModelObserver{ public function create($blog){ } public function update($blog){ } public function delete($blog){ } public function restore($blog){ } public function forceDelete($blog){ } }
- "create"函数将在创建博客模型的数据后加载。
- "update"函数将在更新博客模型的数据后加载。
- "delete"函数将在删除博客模型的数据后加载。
- "restore"函数将在恢复软删除的博客模型数据后加载。
- "forceDelete"函数将在强制删除博客模型数据后加载。
您可以在创建“ichi”文件后,通过终端创建观察者,正如我们在从命令行创建模型中提到的。
php ichi make:observer BlogObserver
观察者的默认路径是“app/Observers”。您也可以在“ichi”文件中修改这个路径。
$modelCommand=new ModelCommand; $modelCommand->setObserverPath('new_app/Observers'); $modelCommand->run(__DIR__,$argv);
创建观察者后,您必须执行
use App\Models\Blog; use App\Observers\BlogObserver; Blog::observe(new BlogObserver);
您还可以为单个模型添加多个观察者
use App\Models\Blog; use App\Observers\{BlogObserver,BlogDataObserver}; Blog::observe(new BlogObserver); Blog::observe(new BlogDataObserver);
观察者的函数将依次加载。
如果您想观察您自定义的函数
在模型中
namespace App\Models; use JiJiHoHoCoCo\IchiORM\Database\Model; class Blog extends Model{ publilc $id,$author_id,$content,$created_at,$updated_at,$deleted_at; public function customFunction(){ /*----- your business logic -----*/ //--- Example to pass one parameter into observer function ---// $currentObject=$this; self::$observerSubject->use(get_class($this),'customFunction',$currentObject); } }
在观察者中
namespace App\Observers; use JiJiHoHoCoCo\IchiORM\Observer\ModelObserver; use App\Models\Blog; class BlogObserver implements ModelObserver{ public function customFunction($blog){ } }
如果需要在观察者函数中传递多个参数。
在模型中
namespace App\Models; use JiJiHoHoCoCo\IchiORM\Database\Model; class Blog extends Model{ publilc $id,$author_id,$content,$created_at,$updated_at,$deleted_at; public function author(){ return $this->refersTo('App\Models\Author','author_id'); } public function customFunction(){ /*----- your business logic -----*/ //--- Example to pass multiple parameter into observer function ---// $currentObject=$this; $author=$this->author(); self::$observerSubject->use(get_class($this),'customFunction',[$currentObject,$author]); } }
在观察者中
namespace App\Observers; use JiJiHoHoCoCo\IchiORM\Observer\ModelObserver; use App\Models\{Blog,Author}; class BlogObserver implements ModelObserver{ public function customFunction($blog,$author){ } }