Code Monkey home page Code Monkey logo

ci4-dynamic-model's Introduction

DynaModel

Dynamic Model for CodeIgniter 4

Build Status Coverage Status

Feature

  • Dynamically Creating the Model on-the-fly
  • Support One-to-One / Many-to-One relationship
  • Support One-to-Many relationship
  • Can Filter data based on child/related criteria
  • Can set the order of One-to-Many relationship result based on child criteria

Installation

composer require arif-rh/ci4-dynamic-model

Usage

Simple

    // creating postModel on-the-fly, just pass a table name
    $postModel = \Arifrh\DynaModel\DB::table('posts');

    // then you can use it, to get all posts
    $postModel->findAll();

Many-to-One Relationship

    // assume that posts always belongs to one author using author_id 
    $postModel->belongsTo('authors);

    // then you can grab author info along with posts
    $postModel->with('authors')->findAll();

    /**
    * by default, primary key of authors will be omit in the column result
    * because its value already exist in the foregin key of relationship
    * 
    * if authors has same column name with posts, then it will be aliased with prefix "author_"
    * for example, both posts and authors has "rating" column, then it will become author_rating
    */

    // you can call only spesific column if you need, pass it on the second parameters in array
    $postModel->with('authors', ['name', 'rating'])->findAll(); 
    // will display all posts column, plus author name and author rating

    // you can filter posts based on author criteria
    $postModel->with('authors')->whereRelation('authors', ['status' => 'active'])->findAll();
    // will display all posts only from active authors

One-to-Many Relationship

    $postModel->hasMany('comments');

    // this will return posts with all related comments
    $postModel->with('comments')->findAll();

    // you can also filter posts based on comments criteria
    $postModel->with('comments')->whereRelation('comments', ['status' => 'approved'])->findAll();
    // will display all posts with approved comments only

Testing

    composer test

    // or

    composer testdox

    // analyse code
    composer analyse

ci4-dynamic-model's People

Contributors

arif-rh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.