A
A
Artem Prokhorov2021-05-20 22:56:46
Yii
Artem Prokhorov, 2021-05-20 22:56:46

How to filter a selection of values ​​from a table in REST Yii using other fields (not just id)?

There is a url like: back.ru/rest/10
I will get a user with id 10 using it.
But how can I get a user with a name equal to Petya, for example?
Well, that is, what would the link look like this: back.ru/rest/?name=some_nameor something like that. Is this possible at all in rest yii?

My controller:

<?php
namespace backend\controllers;

use yii\rest\ActiveController;
use backend\models\Test;

class RestController extends ActiveController
{
    public $modelClass = Test::class;

    public function checkAccess($action, $model = null, $params = [])
    {
        return true;
    }

    public function behaviors() {
        return [
            'contentNegotiator' => [
                'class' => \yii\filters\ContentNegotiator::class,
                'formatParam' => '_format',
                'formats' => [
                    'application/json' => \yii\web\Response::FORMAT_JSON,
                    'xml' => \yii\web\Response::FORMAT_XML
                ],
            ],
        ];
    }
}


My Model:

<?php
namespace backend\models;

class Test extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
            [['name', 'age'], 'required'],
            ['name', 'string'],
            ['age', 'integer'],
        ];
    }
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Absolutus_Deo, 2021-05-21
@kotcich

You inherit from ActiveController which creates methods out of the box in a methodactions

public function actions()
    {
        return [
            'index' => [
                'class' => 'yii\rest\IndexAction',
                'modelClass' => $this->modelClass,
                'checkAccess' => [$this, 'checkAccess'],
            ],
            'view' => [
                'class' => 'yii\rest\ViewAction',
                'modelClass' => $this->modelClass,
                'checkAccess' => [$this, 'checkAccess'],
            ],
            'create' => [
                'class' => 'yii\rest\CreateAction',
                'modelClass' => $this->modelClass,
                'checkAccess' => [$this, 'checkAccess'],
                'scenario' => $this->createScenario,
            ],
            'update' => [
                'class' => 'yii\rest\UpdateAction',
                'modelClass' => $this->modelClass,
                'checkAccess' => [$this, 'checkAccess'],
                'scenario' => $this->updateScenario,
            ],
            'delete' => [
                'class' => 'yii\rest\DeleteAction',
                'modelClass' => $this->modelClass,
                'checkAccess' => [$this, 'checkAccess'],
            ],
            'options' => [
                'class' => 'yii\rest\OptionsAction',
            ],
        ];
    }

So, a request of the type back.ru/rest/10 - displays detailed information on the record, namely ActionView
In order for you to do the filtering of the kind you wrote, you need to override the index or view method, depending on which one you need to filter , can be done using callback.
'dataFilter' => [
                    'class' => 'yii\data\ActiveDataFilter',
                    'searchModel' => 'path/To/Search,
                ],

In this case, you will retain the ability to filter by default as done in YII2.
In your case, you can do this, override prepareDataProvider(), and in the method itself:
$searchModel = new \app\models\SearchModel();    
return $searchModel->search(\Yii::$app->request->queryParams);

In detail, then:
You can override prepareDataProvider for the index action.
public function actions()
        {
            $actions = parent::actions();
            $actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider'];

            return $actions;
        }

public function prepareDataProvider()
        {
            $searchModel = new \app\models\SearchModel();
            $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
            return $dataProvider;
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question