EExcel 丞燕快速查詢2

EExcel 丞燕快速查詢2
EExcel 丞燕快速查詢2 https://sandk.ffbizs.com/

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

namespace App\Controllers\Order;

class OrderListController extends \App\Controllers\BaseController
{
  public function OrderList()
  {
      // 3. 
      $order = (new Order)->where("id", $order_id);
      $orderData = $order->cso()->findAll(); // Maybe need orderData to do something.

      $member = $order->members()->where('createtime >', '2022/11/22')->first(); // ->findAll();
  }
}