Php Yi2 rest api中模型的不同文件
我在yii2中为restapi创建了自定义操作 我的代码是:Php Yi2 rest api中模型的不同文件,php,rest,yii2,yii2-advanced-app,yii2-basic-app,Php,Rest,Yii2,Yii2 Advanced App,Yii2 Basic App,我在yii2中为restapi创建了自定义操作 我的代码是: namespace app\controllers; use yii\rest\ActiveController; use yii\web\Response; use Yii; class RsController extends ActiveController{ public $modelClass='app\models\Mymodel'; /*some another actions*/ publi
namespace app\controllers;
use yii\rest\ActiveController;
use yii\web\Response;
use Yii;
class RsController extends ActiveController{
public $modelClass='app\models\Mymodel';
/*some another actions*/
public function actionOne($id){
return \app\models\Anothermodel::findAll(['my_id'=>$id]);
}
public function actionTwo($id){
return \app\models\Anothermodel::findAll(['my_name'=>'xxxx']);
}
}
我知道我们可以覆盖模型中的字段函数,以获得特殊字段,但
现在我想为actionOne和actionTwo(模型的)获取不同的字段
为此,我如何覆盖Anothermodel中的字段函数?我从
我创建了这样一个组件
<?php
namespace app\components;
class Serializer extends \yii\rest\Serializer {
public $defaultFields;
public $defaultExpand;
public function init() {
parent::init();
$this->defaultFields = !is_null($this->defaultFields) ? implode(",", $this->defaultFields) : $this->defaultFields;
$this->defaultExpand = !is_null($this->defaultExpand) ? implode(",", $this->defaultExpand) : $this->defaultExpand;
}
protected function getRequestedFields() {
$fields = is_null($this->request->get($this->fieldsParam)) ? $this->defaultFields : $this->request->get($this->fieldsParam);
$expand = is_null($this->request->get($this->expandParam)) ? $this->defaultExpand : $this->request->get($this->expandParam);
return [
preg_split('/\s*,\s*/', $fields, -1, PREG_SPLIT_NO_EMPTY),
preg_split('/\s*,\s*/', $expand, -1, PREG_SPLIT_NO_EMPTY),
];
}
}
我建议使用
在ActiveRecord的内部(在我的例子中是这样的)检查fields()方法中的场景
在check fields()方法中检查我的答案:我知道字段,但我不知道如何获得我的特殊目的输出数据与actionOne和actionTwo不同的特殊条件是什么?它与控制器相关,但当我尝试在field函数中获取它们时,我的控制器名称为空。fields()根据其内部成员返回不同的输出。我认为您应该为两个不同的操作创建不同的模型(从相同的基础模型继承)。否则,您可以从ActiveRecord find方法获得返回,然后遍历它以填充特定属性,您可以在其fields()方法中检查该属性
public function actionOne($id){
$this->serializer['defaultFields'] = ["field1",
"field2"];
return new \yii\data\ActiveDataProvider([
'query' => \app\models\Anothermodel::find()->where(['my_id'=>$id]),
]);
}
public function actionTwo($id){
$this->serializer['defaultFields'] = ["field1",
"field2","field3"];
return new \yii\data\ActiveDataProvider([
'query' => \app\models\Anothermodel::find()->where(['my_id'=>$id]),
]);
}
public function actionPublic()
{
\yii\base\Event::on(Thing::class, Thing::EVENT_AFTER_FIND, function ($event) {
$event->sender->scenario = Thing::SCENARIO_SEARCH_PUBLIC;
});
return new ActiveDataProvider([
'query' => Thing::find(),
]);
}
public function actionPrivate()
{
\yii\base\Event::on(Thing::class, Thing::EVENT_AFTER_FIND, function ($event) {
$event->sender->scenario = Thing::SCENARIO_SEARCH_PRIVATE;
});
return new ActiveDataProvider([
'query' => Thing::find(),
]);
}
public function fields()
{
$fields = parent::fields();
if ($this->scenario === self::SCENARIO_SEARCH_PUBLIC) {
unset($fields['field1'], $fields['field2'], $fields['field3'], $fields['field4']);
}
return $fields;
}