Yii2的restfullAPI用filter来检索数据
2022-10-08 本文已影响0人
itBox
默认的Yii2架构好restfull api 之后,发现get是不支持filter检索,各种配置测试不行,还得是google,从这篇官方文档找到了答案:
https://www.yiiframework.com/doc/guide/2.0/en/rest-filtering-collections
核心就是在 controller 加上并配置datafilter属性
几个问题就是
DynamicModel 是个啥?
attributeMap 又是啥?
public function actions()
{
$actions = parent::actions();
$actions['index']['dataFilter'] = [
'class' => \yii\data\ActiveDataFilter::class,
'attributeMap' => [
'clockIn' => 'clock_in',
],
'searchModel' => (new DynamicModel(['id', 'clockIn']))->addRule(['id', 'clockIn'], 'integer', ['min' => 1]),
];
return $actions;
}
PS:Yii2的版本要在 2.0.13 以上才支持