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

SeckillSolution 秒殺 高并發 交易

https://github.com/qianshou/SeckillSolution https://blog.csdn.net/koastal/article/details/78995885 ===== https://blog.csdn.net/qq_38512763/article/details/118830903

2022-09-30 · 1 min · 4 words · Me

Aggregate array values into ranges

https://codereview.stackexchange.com/questions/80080/aggregate-array-values-into-ranges // Output: // Array ( // [0] => Array ( [0] => 1, [1] => 2, [2] => 3, [3] => 4, [4] => 5, [5] => 6 ) // [1] => Array ( [0] => 10, [1] => 11, [2] => 12, [3] => 13 ) // [2] => Array ( [0] => 20 ) // [3] => Array ( [0] => 24 ) // ) static function GetRanges1( $aNumbers ) { $aNumbers = array_unique( $aNumbers ); sort( $aNumbers ); $aGroups = array(); for( $i = 0; $i < count( $aNumbers ); $i++ ) { if( $i > 0 && ( $aNumbers[$i-1] == $aNumbers[$i] - 1 )) array_push( $aGroups[count($aGroups)-1], $aNumbers[$i] ); else array_push( $aGroups, array( $aNumbers[$i] )); } return $aGroups; } // Output: Array ( [0] => 1-6, [1] => 10-13, [2] => 20, [3] => 24 ) static function GetRanges2( $aNumbers ) { $aNumbers = array_unique( $aNumbers ); sort( $aNumbers ); $aGroups = array(); for( $i = 0; $i < count( $aNumbers ); $i++ ) { if( $i > 0 && ( $aNumbers[$i-1] == $aNumbers[$i] - 1 )) array_push( $aGroups[count($aGroups)-1], $aNumbers[$i] ); else array_push( $aGroups, array( $aNumbers[$i] )); } $aRanges = array(); foreach( $aGroups as $aGroup ) { if( count( $aGroup ) == 1 ) $aRanges[] = $aGroup[0]; else $aRanges[] = $aGroup[0] . '-' . $aGroup[count($aGroup)-1]; } return $aRanges; } // Output: 1~6,10~13,20,24 static function GetRanges3( $aNumbers, $headChar='' ) { $aNumbers = array_unique( $aNumbers ); sort( $aNumbers ); $aGroups = array(); for( $i = 0; $i < count( $aNumbers ); $i++ ) { if( $i > 0 && ( $aNumbers[$i-1] == $aNumbers[$i] - 1 )) array_push( $aGroups[count($aGroups)-1], $aNumbers[$i] ); else array_push( $aGroups, array( $aNumbers[$i] )); } $aRanges = array(); foreach( $aGroups as $aGroup ) { if( count( $aGroup ) == 1 ) $aRanges[] = $headChar.$aGroup[0]; else $aRanges[] = $headChar.$aGroup[0] . '~' . $headChar.$aGroup[count($aGroup)-1]; } return implode( ',', $aRanges ); }

2022-09-23 · 2 min · 313 words · Me

[轉]PHP 8.2 Remove libmysql mysqli

https://m.facebook.com/story.php?story_fbid=10216824504164331&id=1815507975 《 PHP RFC: Remove support for libmysql from mysqli 》 » https://wiki.php.net/rfc/mysqli_support_for_libmysql PHP 核心開發團隊投票通過移除 mysqli 的 libmysql 支援,將於 PHP 8.2 正式生效。這項討論從農曆年前關注到年後,最終於 2022/2/5 全數投票通過。 對於一般 PHP 開發者是好事,不用再考慮 MySQL 是選擇 libmysql 還是 mysqlnd;面試時也減少面試官詢問兩者差異的比較 (不過現在很多面試官也不知道了)。 如果想瞭解 libmysql / mysqlnd 的優缺點,官方 RFC 也貼心地條列整理了。不過 RFC 裡沒提關於「License (授權)」的考量,特別在商業上。這也是 PHP 與 Python 及 Ruby 等社群有著不太一樣的生態考量。 Python 要連結 MySQL,通常選用 MySQL Connector 或 MySQLdb,但這兩者底層都依賴 libmysqlclient (MySQL C Library),而 libmysqlclient 的授權 [1] 主要採用 GPL-2.0,進而連帶影響了整體產品/專案的授權。 Ruby 要連結 MySQL,通常選用 mysql2,而其底層同樣依賴 libmysqlclient,有著同樣的潛在商業問題。 更重要的是 libmysqlclient 背後的公司是 Oracle。 ...

2022-02-07 · 1 min · 115 words · Me

zendesk opensource freescout

https://github.com/freescout-helpdesk/freescout

2021-03-12 · 1 min · word · Me