1、在 /qq/rests/article_category/StandardIndexAction.php 中
/* @var $modelClass /yii/db/BaseActiveRecord */ $modelClass = $this->modelClass; $query = $modelClass::find() ->joinWith('qqArticleCategoryNormal') ->where([ $modelClass::tableName() . '.is_deleted' => $modelClass::IS_DELETED_NO, QqArticleCategoryNormal::tableName() . '.is_deleted' => QqArticleCategoryNormal::IS_DELETED_NO, ]) ->asArray() ->orderBy([$modelClass::tableName() . '.id' => SORT_DESC]); if (!empty($filter)) { $query->andFilterWhere($filter); } // 设置每页资源数量默认为资源总数 $count = (int) $query->count($modelClass::tableName() . '.id'); if (empty($requestParams['per-page'])) { $requestParams['per-page'] = $count; } return Yii::createObject([ 'class' => ActiveDataProvider::className(), 'query' => $query, 'pagination' => [ 'params' => $requestParams, 'pageSizeLimit' => [1, $count], ], 'sort' => [ 'params' => $requestParams, ], ]);
2、期待的响应结果如下:
"items": [ { "id": 226, "name": "其他综艺", "parent_id": 0, "status": 1, "is_deleted": 0, "created_at": 1542178227, "updated_at": 1542178227, "deleted_at": 0 }, { "id": 225, "name": "舞台剧", "parent_id": 0, "status": 1, "is_deleted": 0, "created_at": 1542178227, "updated_at": 1542178227, "deleted_at": 0 } ]
3、实际的响应结果如下:所有字段类型皆为字符串,如图1
"items": [ { "id": "67", "name": "跑步", "parent_id": "0", "status": "1", "is_deleted": "0", "created_at": "1542178227", "updated_at": "1542178227", "deleted_at": "0" }, { "id": "66", "name": "健身", "parent_id": "0", "status": "1", "is_deleted": "0", "created_at": "1542178227", "updated_at": "1542178227", "deleted_at": "0" } ]
4、编辑 /qq/rests/article_category/Serializer.php,打印 $models
class Serializer extends /yii/rest/Serializer { /** * Serializes a data provider. * @param DataProviderInterface $dataProvider * @return array the array representation of the data provider. */ protected function serializeDataProvider($dataProvider) { if ($this->preserveKeys) { $models = $dataProvider->getModels(); } else { $models = array_values($dataProvider->getModels()); } $models = $this->serializeModels($models); var_dump($models); exit; } }
5、打印结果如下,所有字段皆为字符串
array(67) { [0]=> array(9) { ["id"]=> string(2) "67" ["name"]=> string(6) "跑步" ["parent_id"]=> string(1) "0" ["status"]=> string(1) "1" ["is_deleted"]=> string(1) "0" ["created_at"]=> string(10) "1542178227" ["updated_at"]=> string(10) "1542178227" ["deleted_at"]=> string(1) "0" ["qqArticleCategoryNormal"]=> array(8) { ["id"]=> string(3) "334" ["article_category_id"]=> string(2) "67" ["name"]=> string(6) "跑步" ["status"]=> string(1) "1" ["is_deleted"]=> string(1) "0" ["created_at"]=> string(10) "1542178227" ["updated_at"]=> string(10) "1542178227" ["deleted_at"]=> string(1) "0" } } }
6、编辑 /qq/rests/article_category/StandardIndexAction.php,删除 ->asArray(),因为仅需要当前模型的数据,不需要关联模型的数据(即 qqArticleCategoryNormal)
/* @var $modelClass /yii/db/BaseActiveRecord */ $modelClass = $this->modelClass; $query = $modelClass::find() ->joinWith('qqArticleCategoryNormal') ->where([ $modelClass::tableName() . '.is_deleted' => $modelClass::IS_DELETED_NO, QqArticleCategoryNormal::tableName() . '.is_deleted' => QqArticleCategoryNormal::IS_DELETED_NO, ]) ->orderBy([$modelClass::tableName() . '.id' => SORT_DESC]); if (!empty($filter)) { $query->andFilterWhere($filter); } // 设置每页资源数量默认为资源总数 $count = (int) $query->count($modelClass::tableName() . '.id'); if (empty($requestParams['per-page'])) { $requestParams['per-page'] = $count; } return Yii::createObject([ 'class' => ActiveDataProvider::className(), 'query' => $query, 'pagination' => [ 'params' => $requestParams, 'pageSizeLimit' => [1, $count], ], 'sort' => [ 'params' => $requestParams, ], ]);
7、实际的响应结果如下:字段类型与数据库中一致,符合预期,如图2
"items": [ { "id": 67, "name": "跑步", "parent_id": 0, "status": 1, "is_deleted": 0, "created_at": 1542178227, "updated_at": 1542178227, "deleted_at": 0 }, { "id": 66, "name": "健身", "parent_id": 0, "status": 1, "is_deleted": 0, "created_at": 1542178227, "updated_at": 1542178227, "deleted_at": 0 } ]
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/250442.html