Why ActiveController can't filter records?

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…

Best Regards.

You can filter models by adding a filter model and pass the get param (or body param) filter to the rest route: https://www.yiiframework.com/doc/guide/2.0/en/rest-resources#filtering-collections

I must admit, at first sight it looks way to complex but once understand you will be able to filter for any condition while requesting the data (https://www.yiiframework.com/doc/guide/2.0/en/output-data-providers#filtering-data-providers-using-data-filters).

may this helps, i was not sure if i understood your question correctly :slight_smile:

hi @nadario do you hace some example? I can’t get it work.
Best Regards

Maybe show us what you have already done.

Fine!

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']);
    }
}

When I go to : http://localhost/atadv/api/web/localidad?filter[descripcion][like]=SAN

I get a list of “Localidad” entities (models)
but I can’t make the filter work

I comment some lines in the controller, because it fails when trying to run search() method…
I think i’m missing one step…

Best Regards