Hi, i’m quite new to REST, I wondering why REST in YII 2, can’t filter records, like other Controllers ?
I have to search Google and follow this instructions to filter records
I guess that making a GET to a table in real world should expect a bunch of data, it is clear that you need to filter data before returning…
First off all, I created an “api” folder, like frontend, so I can have separated controllers and models.
This is my controller:
namespace api\controllers;
use yii\rest\ActiveController;
class LocalidadController extends ActiveController
{
public $modelClass = 'app\models\Localidad';
public function actions()
{
$actions = parent::actions();
//$actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider'];
return $actions;
}
public function prepareDataProvider()
{
$searchModel = new \app\models\Localidad();
//return $searchModel->search(\Yii::$app->request->queryParams);
}
public function formName()
{
return '';
}
}
this is my Model ( Localidad )
namespace app\models;
use Yii;
/**
* This is the model class for table "localidades".
*
* @property int $id
* @property string $descripcion
*
* @property Ensayos[] $ensayos
* @property Ensayos[] $ensayos0
* @property Ensayos[] $ensayos1
*/
class Localidad extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'localidades';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['descripcion'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'descripcion' => 'Descripcion',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEnsayos()
{
return $this->hasMany(Ensayos::className(), ['localidad_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEnsayos0()
{
return $this->hasMany(Ensayos::className(), ['localidad_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEnsayos1()
{
return $this->hasMany(Ensayos::className(), ['localidad_id' => 'id']);
}
}