Codeigniter 4.2 with Eloquent ORM

composer require "illuminate/database" composer require "illuminate/events" Config/Events.php use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Events\Dispatcher; use Illuminate\Container\Container; function boostEloquent() { $capsule = new Capsule; $capsule->addConnection([ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database', 'username' => 'root', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ]); // Set the event dispatcher used by Eloquent models... (optional) $capsule->setEventDispatcher(new Dispatcher(new Container)); // Make this Capsule instance available globally via static methods... (optional) $capsule->setAsGlobal(); // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) $capsule->bootEloquent(); } Events::on('pre_system', function () { boostEloquent(); if (ENVIRONMENT !== 'testing') ... } https://www.modb.pro/db/49966

2023-05-08 · 1 min · 97 words · Me

Codeigniter 4 ORM relation By hand Part 2

Modify ModelTrait file at \App\Traits namespace App\Traits; trait ModelTrait { // fetaure Maybe belongsTo // get result but not effect this, keep this ORM status public function newSelfObject() { return $this->db->query($this->builder->getCompiledSelect(false)); } // copy self object return ORM Object public function cso() { $class_name = get_class($this); $class_new_object = (new $class_name); $class_new_object->builder = clone $this->builder; return $class_new_object; } public function hasOne($class, $relation_primary_key=null, $primary_key=null) { return $class->where($relation_primary_key ?? $this->primaryKey, $this->newSelfObject()->getRowArray()[$primary_key ?? $this->primaryKey] ?? ''); } // whereIn If can't get records, return null. Usually use primaryKey cloud happen public function hasMany($class, $relation_primary_key=null, $primary_key=null) { $temp = array_column($this->newSelfObject()->getResult(), $primary_key ?? $this->primaryKey); return $class->whereIn($relation_primary_key ?? $this->primaryKey, empty($temp) ? [null] : $temp); } } App\Controllers\OrderListContriller.php ...

2022-12-29 · 1 min · 147 words · Me

Codeigniter 4 ORM relation By hand

Add ModelTrait file at \App\Traits namespace App\Traits; trait ModelTrait { // fetaure Maybe belongsTo // get result but not effect this, keep this ORM status public function newSelfObject() { return $this->db->query($this->builder->getCompiledSelect(false)); } public function hasOne($class, $relation_primary_key=null, $primary_key=null) { return $class->where($relation_primary_key ?? $this->primaryKey, $this->newSelfObject()->getRowArray()[$primary_key ?? $this->primaryKey] ?? ''); } // whereIn If can't get records, return null. Usually use primaryKey cloud happen public function hasMany($class, $relation_primary_key=null, $primary_key=null) { $temp = array_column($this->newSelfObject()->getResult(), $primary_key ?? $this->primaryKey); return $class->whereIn($relation_primary_key ?? $this->primaryKey, empty($temp) ? [null] : $temp); } } How to use App\Models\Order.php class Order extends Model { use ModelTrait; protected $table = 'order'; protected $primaryKey = 'order_id'; public function members() { return $this->hasMany(new Member); // OR return $this->hasMany(new Member, 'id', 'member_id'); // id always use new Member, member_id use Order } App\Controllers\OrderListContriller.php ...

2022-11-22 · 1 min · 201 words · Me