immachakata/ci4-relationships 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

immachakata/ci4-relationships

最新稳定版本:v1.2-alpha.1

Composer 安装命令:

composer require immachakata/ci4-relationships

包简介

Easily add and manage database relationships in CodeIgniter 4

README 文档

README

A simple solution to add relationships to your Codeigniter 4 application.

Installation

You can install this library from composer by running composer install immachakata/ci4-relationships

Defining Relationships

You can define as many relationships as you want using the hasMany or hasOne functions as needed. Here's an example of defining a single relationship for a users avatar and multiple relationship for a users links.

namespace App\Models;

use CodeIgniter\Model;
use CI4Extensions\Database\RelationshipsTrait;

class UserModel extends Model
{
    use RelationshipsTrait;
    
    ... 
    // declare relationships
    public function initialize(){
        $this->hasOne('avatar', AvatarModel::class, 'user_id');
        $this->hasMany('links', LinkModel::class, 'user_id');
    }
}

Using Relationships

When you perfom your query, explicitly mention the field name you want back as you defined in the initialize function. To do so, use the with($fieldName) function. Here's an example of retrieving the user avatar

class UserController extends BaseController
{
    public function index(){
        ...
        $this->respond([
            'user' => model(UserModel::class)->with('avatar')->findAll()
        ]);
    }
}

Credits

Forked from michalsn/codeigniter-nested-model

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固