server/app/Model/Translation.php

84 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Db;
/**
* @property int $translation_id
* @property string $group
* @property string $translation_key
* @property string $remark
* @property string $create_time
* @property string $update_time
* @property string $deleted_at
*/
class Translation extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'translation';
protected string $primaryKey = "translation_id";
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['translation_id' => 'integer'];
public static function list(array $param)
{
$model = self::query()->with("values");
if (isset($param['group']) && $param['group'] != '') {
$model = $model->where('group', $param['group']);
}
if (isset($param['translation_key']) && $param['translation_key'] != '') {
$model = $model->where('translation_key', $param['translation_key']);
}
return $model->orderByDesc("translation_id")
->select(["translation_id", "group", "translation_key", "remark", "create_time", "update_time"])
->paginate((int)$param['limit']);
}
public function values()
{
return $this->hasMany(TranslationValue::class, 'translation_id', 'translation_id');
}
public static function add($data): bool
{
$values = $data['values'];
unset($data['values']);
// 更新主表
$id = parent::add($data);
if (!$id) {
return false;
}
// 更新子表
TranslationValue::setValue($id, $values);
return true;
}
public static function edit($data): bool
{
$values = $data['values'];
unset($data['values']);
// 更新主表
$res = parent::edit($data);
if (!$res) {
return false;
}
// 更新子表
TranslationValue::setValue($data['translation_id'], $values);
return true;
}
}