46 lines
1.4 KiB
PHP
46 lines
1.4 KiB
PHP
<?php
|
||
|
||
namespace App\Cache\Base;
|
||
|
||
use App\Models\Follow\FollowModel;
|
||
|
||
//用户缓存用户基础信息(等于数据表)primary_key为主键
|
||
abstract class TableBaseCache extends BaseCache
|
||
{
|
||
public string $table_class;
|
||
public string $primary_key_column = 'uid';
|
||
public array $get_columns = ['*'];
|
||
public array $get_exclude_columns = [];
|
||
|
||
function loadTable(): array|null
|
||
{
|
||
return null;
|
||
}
|
||
|
||
function loadData(): array|null
|
||
{
|
||
//默认获取表数据方式
|
||
if (!empty($table_class)) {
|
||
$oTable = new $this->table_class();
|
||
//排除不需要的字段
|
||
if (!empty($this->get_exclude_columns) && $this->get_columns != ['*']) $this->get_columns = array_diff($this->get_columns, $this->get_exclude_columns);
|
||
$oData = $oTable->findItemByWhere([$this->primary_key_column => $this->primary_key], $this->get_columns);
|
||
if($oData->isEmpty()) return null;
|
||
$aData = $oData->toArray();
|
||
//排除不需要的字段
|
||
if (!empty($this->get_exclude_columns) && $this->get_columns == ['*'] && !empty($aData)) {
|
||
foreach ($this->get_exclude_columns as $sColumn) {
|
||
unset($aData[$sColumn]);
|
||
}
|
||
}
|
||
|
||
if (empty($aData)) return null;
|
||
return $aData;
|
||
} else {
|
||
return $this->loadTable();
|
||
}
|
||
}
|
||
|
||
|
||
}
|