ignitor/activerecord

Better ORM for CodeIgniter 4

dev-main 2025-04-27 18:21 UTC

This package is auto-updated.

Last update: 2025-04-27 18:23:31 UTC


README

This is a database ActiveRecord Implementation for CodeIgniter 4.

Installation

You can install the package via composer:

composer require ignitor/activerecord:^1.0@dev

Usage

Create a Model

<?php

namespace App\Models;

use Ignitor\ActiveRecord\Model;

class User extends Model
{
    
}

Inserting a record

$user = new User();
$user->name = 'John Doe';
$user->email = 'john@example.com';
$user->save();

Updating a record

$user = User::find(1);
$user->name = 'Jane Doe';
$user->save();

Deleting a record

$user = User::find(1);
$user->delete();

Querying a record

$user = User::query()->where('name', 'John Doe')->first();

Querying a record with conditions

$user = User::query()->where('name', 'John Doe')->where('email', 'john@example.com')->first();

Querying a record with order

$user = User::query()->orderBy('name', 'desc')->first(); // DESC

Querying a record with limit

$user = User::query()->limit(10)->get();

Querying a record with offset

$user = User::query()->offset(10)->get();

Querying a record with group

$user = User::query()->groupBy('name')->get();

Querying a record with join

$user = User::query()->join('posts', 'users.id', '=', 'posts.user_id')->get();

Querying a record with left join

$user = User::query()->leftJoin('posts', 'users.id', '=', 'posts.user_id')->get();

Querying a record with right join

$user = User::query()->rightJoin('posts', 'users.id', '=', 'posts.user_id')->get();

Querying a record with inner join

$user = User::query()->innerJoin('posts', 'users.id', '=', 'posts.user_id')->get();

Querying a record with cross join

$user = User::query()->crossJoin('posts', 'users.id', '=', 'posts.user_id')->get();

Querying a record with raw query

$user = User::raw('SELECT * FROM users WHERE name = ?', ['John Doe'])->get();

Querying a record with raw query with bindings

$user = User::raw('SELECT * FROM users WHERE name = ?', ['John Doe'])->get();

Querying a record with exists

$user = User::query()->whereExists(function ($query) {
    $query->select('id')->from('posts')->where('user_id', 1);
})->get();

Querying a record with not exists

$user = User::query()->whereNotExists(function ($query) {
    $query->select('id')->from('posts')->where('user_id', 1);
})->get();

Querying a record with in

$user = User::query()->whereIn('id', [1, 2, 3])->get();

Querying a record with not in

$user = User::query()->whereNotIn('id', [1, 2, 3])->get();

Querying a record with between

$user = User::query()->whereBetween('id', [1, 100])->get();

Querying a record with not between

$user = User::query()->whereNotBetween('id', [1, 100])->get();

Querying a record with like

$user = User::query()->whereLike('name', '%John%')->get();

Querying a record with not like

$user = User::query()->whereNotLike('name', '%John%')->get();

Querying a record with ilike

$user = User::query()->whereIlike('name', '%John%')->get();

Querying a record with not ilike

$user = User::query()->whereNotIlike('name', '%John%')->get();

Querying a record with is null

$user = User::query()->whereNull('name')->get();

Querying a record with is not null

$user = User::query()->whereNotNull('name')->get();

Querying a record with exists

$user = User::query()->whereExists(function ($query) {
    $query->select('id')->from('posts')->where('user_id', 1);
})->get();

Querying a record with not exists

$user = User::query()->whereNotExists(function ($query) {
    $query->select('id')->from('posts')->where('user_id', 1);
})->get();

Querying a record with having exists

$user = User::query()->whereHavingExists(function ($query) {
    $query->select('id')->from('posts')->where('user_id', 1);
})->get();

Querying a record with having not exists

$user = User::query()->whereHavingNotExists(function ($query) {
    $query->select('id')->from('posts')->where('user_id', 1);
})->get();

Defining a relationship (one to one)

class User extends Model
{
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}

Defining a reverse relationship (one to one)

class User extends Model
{
    public function profile()
    {
        return $this->belongsTo(Profile::class);
    }
}

Defining a relationship (one to many)

class User extends Model
{
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

Defining a relationship with conditions

class User extends Model
{
    public function posts()
    {
        return $this->hasMany(Post::class)->where('published', true);
    }
}

Defining a relationship with limit

class User extends Model
{
    public function posts()
    {
        return $this->hasMany(Post::class)->limit(10)->offset(10)->groupBy('created_at')->orderBy('created_at', 'desc');
    }
}

Defining a relationship with join

class User extends Model
{
    public function posts()
    {
        return $this->hasMany(Post::class)->join('comments', 'posts.id', '=', 'comments.post_id');
    }
}

Defining a morph relationship (one to one)

class User extends Model
{
    public function profile()
    {
        return $this->morphOne(Profile::class, 'profileable');
    }
}

Defining a morph relationship (one to many)

class User extends Model
{
    public function posts()
    {
        return $this->morphMany(Post::class, 'postable');
    }
}

Reverse defining a morph relationship (one to one)

class Photo extends Model
{
    public function imageable()
    {
        return $this->morphTo();
    }
}

Querying a record with a relationship (Lazy Loading)

$user = User::query()->with('profile')->first();

Querying a record with a relationship with conditions (Lazy Loading)

$user = User::query()->where('name', 'John Doe')->first();

// access the relationship
$user->profile;

Querying a record with a relationship (Eager Loading)

$user = User::query()->where('name', 'John Doe')->with('profile')->first();

// access the relationship
$user->profile;

Working with complex queries without the query builder

$user = User::query()->where(function ($query) {
    $query->where('name', 'John Doe');
    $query->orWhere('email', 'john@example.com');
})->first();

//