#rest #api #yii2
#rest #API #yii2
Вопрос:
Когда я запрашиваю localhost / advanced / frontend /web/ todos?username=bahti, он выдает соответствующий идентификатор, но правильное имя пользователя — bahtiyar. Это не должен быть идентификатор ответа. Кто знает, в чем проблема?
этот идентификатор моего контроллера
<?php
namespace frontendcontrollers;
use yiirestActiveController;
class TodoController extends ActiveController
{
public $modelClass = 'frontendmodelsUsers';
public function actions() {
$actions = parent::actions();
$actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider'];
return $actions;
}
public function prepareDataProvider() {
$searchModel = new frontendmodelsusersSearch();
return $searchModel->search(Yii::$app->request->queryParams);
}
}
Это моя модель
namespace frontendmodels;
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 yiidbActiveRecord
{
/**
* @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 frontendmodels;
use Yii;
use yiibaseModel;
use yiidataActiveDataProvider;
use frontendmodelsusers;
/**
* usersSearch represents the model behind the search form about `frontendmodelsusers`.
*/
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:
@Bahtiyar, проблема в приведенном ниже коде:
$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]);
Вы использовали like, поэтому, когда вы запрашиваете bahti, он возвращает соответствующий идентификатор. Давайте удалим оператор « like
«, используем оператор « =>
«