| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace addons\OperationCenter\common\models;
- use yii\helpers\Json;
- use yii\data\Pagination;
- use addons\OperationCenter\common\models\traits\SoftDeleteTrait;
- use common\models\base\BaseActiveRecord as BaseBaseActiveRecord;
- /**
- * BaseActiveRecord class
- * @package addons\OperationCenter\common\models
- */
- class BaseActiveRecord extends BaseBaseActiveRecord
- {
- use SoftDeleteTrait;
- /**
- * 获取分页数据
- *
- * @param integer $page
- * @param integer $limit
- * @param callable $callback
- * @param string $order_by
- * @return array
- */
- public static function getPageList(
- int $page, int $limit, callable $callback, string $order_by = 'id desc'
- ) :array
- {
- $model = static::find();
- if (!empty($callback)) call_user_func($callback, $model);
- $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
- $model->limit($pagination->limit)->offset($pagination->offset);
- $list = $model->asArray()->orderBy($order_by)->all();
- $data['list'] = $list;
- $data['page_count'] = $pagination->pageCount;
- $data['current_page'] = $page + 1;
- return $data;
- }
- /**
- * @param array $data
- * @return array
- */
- public function jsonEncode(array $data)
- {
- $json_keys = $this->json_keys ?? [];
- foreach ($json_keys as $key) {
- // !empty($data[$key]) && ($data[$key] = Json::encode($data[$key]));
- // 默认
- $data[$key] = !empty($data[$key]) ? $data[$key] : [];
- $data[$key] = Json::encode($data[$key]);
- }
- return $data;
- }
- /**
- * @return static
- */
- public function jsonDecode()
- {
- $json_keys = $this->json_keys ?? [];
- foreach ($json_keys as $key) {
- !empty($this->$key) && ($this->$key = Json::decode($this->$key));
- }
- return $this;
- }
- /**
- * @param boolean $runValidation
- * @param array $attributeNames
- * @return true|static
- */
- public function save($runValidation = true, $attributeNames = null)
- {
- $res = parent::save($runValidation = true, $attributeNames = null);
- return $res === true
- ? true
- : $this;
- }
- }
|