承接 myth/db 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

myth/db

Composer 安装命令:

composer require myth/db

包简介

An effortless database abstraction built on top of CodeIgniter 4.

README 文档

README

Myth:db is an exploration of a possible new database layer for CodeIgniter 4. It adds a reflective Data Mapper to make getting data as simple as possible. While it uses Models and Entities in the background, it can be used with almost zero setup or code generation.

Examples

Getting all results in a table

The basic usage is with the map() method, which takes the name of the table you want to work with. It then creates a generic model, assigns the table name, inspects the table to populate the model's properties, and returns the model. Since you have a model, you then have access to the model methods, such as findAll(), and the Query Builder methods, such as where().

$users = db()->map('users')->findAll();

By default, this would return an array of objects, one for each of the rows in the table. If you would like to change the return type, you can pass in the name of the class, array, or object with the returns() method.

$users = db()->map('users')->returns('array')->findAll();
$users = db()->map('users')->returns(\App\Entities\User::class)->findAll();

Custom Entities

You can create your own Entities and the mapper will automatically return results in that format. This is all based on conventions. By default, it would look for an entity named User in the App\Entities namespace. The name of the Entity is the singular, PascalCase version of the table name.

// If App\Entities\User exists, all results will be returned as User objects
// otherwise, each record will be returned as an object.
$users = db()->map('users')->findAll();

// Would return each result as instance of App\Entities\ZodiacSign, if it exists.
$signs = db()->map('zodiac_signs')->findAll();

Custom Models

You can also create your own Models and the mapper will automatically use that instead of the default Model. This is also based on conventions. It would look for a model named the PascalCase singular version of the table name, with Model appended to it.

// Will look for App\Models\UserModel
$users = db()->map('users')->findAll();

// Will look for App\Models\ZodiacSignModel
$signs = db()->map('zodiac_signs')->findAll();

NOTE: Returns an iterable cursor for better memory?

TODO: Return findX results as a collection?

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-30