fishingboy/codeigniter-model-base 问题修复 & 功能扩展

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

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

fishingboy/codeigniter-model-base

Composer 安装命令:

composer require fishingboy/codeigniter-model-base

包简介

Codeigniter Model Base

README 文档

README

Packagist Version Downloads License: MIT

English | 繁體中文

Installation

composer require fishingboy/codeigniter-model-base

Requirements

  • CodeIgniter 3
  • Composer autoload enabled in your CodeIgniter application

Usage

If your table name is users, create the model file at application/models/Users_model.php.

<?php

use fishingboy\ci_model_base\CI_Model_base;

class Users_model extends CI_Model_base
{
    protected $table = "users";
}

Load the model before using it:

$this->load->model('Users_model', 'users_model');

Basic Operations

Find

Find records by a single field:

$users = $this->users_model->findBy('email', 'user@example.com');

Find records by multiple conditions:

$users = $this->users_model->findBy([
  'role' => 'admin',
  'status' => 'on'
]);

If the table has a status field, findBy() automatically adds status = 'on'.

Create

Create a record and return the inserted ID:

$id = $this->users_model->create([
  'title' => 'sample'
]);

If the table has created_at or updated_at fields, they are automatically set to NOW() when not provided.

Update

Update a record by primary key and return the updated ID:

$this->users_model->update($id, [
  'title' => 'sample'
]);

The default primary key is id. You can override it in your model:

protected $key = 'user_id';

Update by Conditions

Update records using custom conditions:

$this->users_model->updateWhere([
  'role' => 'member'
], [
  'status' => 'off'
]);

Delete by Conditions

Delete records using custom conditions:

$this->users_model->delete_where([
  'id' => $id
]);

Model Hooks

Override these methods in your model when you need custom validation or follow-up logic.

protected function verifyBeforeCreate(& $params)
{
    return $this->verifyParams($params, [
        'title' => 'required'
    ]);
}

protected function verifyBeforeUpdate(& $params)
{
    return true;
}

protected function verifyBeforeDelete(& $params)
{
    return true;
}

protected function afterCreate($id)
{
    return true;
}

Date Fields

For create(), update(), and updateWhere(), passing NOW() as a field value writes the database NOW() expression instead of a string.

$this->users_model->update($id, [
  'published_at' => 'NOW()'
]);

License

This package is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固