Yii2 rest api获取请求响应数据不正确

时间:2016-10-08 09:56:49

标签: rest api yii2

当我请求localhost / advanced / frontend / web / todos?username = bakhti时,它会响应相应的id,但正确的用户名是bakhtiyar。它不能响应id。谁知道问题出在哪里?

这个我的控制器

<?php

namespace frontend\controllers;

use yii\rest\ActiveController;

class TodoController extends ActiveController
{
    public $modelClass = 'frontend\models\Users';

    public function actions() {

        $actions = parent::actions();
        $actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider'];

        return $actions;
    }

    public function prepareDataProvider() {

        $searchModel = new \frontend\models\usersSearch();    
        return $searchModel->search(\Yii::$app->request->queryParams);
    }

}

这是我的模特     

namespace frontend\models;

use Yii;

/**
 * This is the model class for table "users".
 *
 * @property integer $id
 * @property string $firstname
 * @property string $lastname
 * @property string $email
 * @property string $password
 * @property string $p_image
 * @property string $images
 * @property integer $admin
 */
class Users extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'users';
    }

    public function fields()
    {
        return [
            'id','firstname','lastname'
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['firstname', 'lastname', 'email', 'password', 'p_image', 'images'], 'required'],
            [['admin'], 'integer'],
            [['firstname', 'lastname', 'email', 'password', 'p_image', 'images'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'firstname' => 'Firstname',
            'lastname' => 'Lastname',
            'email' => 'Email',
            'password' => 'Password',
            'p_image' => 'P Image',
            'images' => 'Images',
            'admin' => 'Admin',
        ];
    }

    public function getOrders()
    {
        return $this->hasMany(Order::className(), ['u_id' => 'id']);
    }
}

这是我的搜索模型     

namespace frontend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use frontend\models\users;

/**
 * usersSearch represents the model behind the search form about `frontend\models\users`.
 */
class usersSearch extends users
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'admin'], 'integer'],
            [['firstname', 'lastname', 'email', 'password', 'p_image', 'images'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = users::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params,'');

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'admin' => $this->admin,
        ]);

        $query->andFilterWhere(['like', 'firstname', $this->firstname])
            ->andFilterWhere(['like', 'lastname', $this->lastname])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'password', $this->password])
            ->andFilterWhere(['like', 'p_image', $this->p_image])
            ->andFilterWhere(['like', 'images', $this->images]);

        return $dataProvider;
    }
}

1 个答案:

答案 0 :(得分:0)

@Bakhtiyar,问题出在下面的代码:

$query->andFilterWhere(['like', 'firstname', $this->firstname])
        ->andFilterWhere(['like', 'lastname', $this->lastname])
        ->andFilterWhere(['like', 'email', $this->email])
        ->andFilterWhere(['like', 'password', $this->password])
        ->andFilterWhere(['like', 'p_image', $this->p_image])
        ->andFilterWhere(['like', 'images', $this->images]);

你使用了喜欢,这就是为什么当你请求bakhti时,它会响应相应的id。让我们删除&#34; like&#34;运营商,使用&#34; =>&#34;操作