When you want to set value to your model attributes, please know which to use.
From the API documentation,
load() public method
Populates the model with input data.
You might want to use this when the value are passed by a form with input fields.
setAttributes() public method
Sets the attribute values in a massive way.
You might want to use this when the values are not passed by a form.
Saturday, July 10, 2021
Yii 2 Model load() vs setAttributes()
Friday, July 9, 2021
Yii 2 ActiveQuery ->one()
You can't use ->one() in your query when using ActiveDataProvider. It assumes that your query returns a list of records.
public function actionIndex()
{
$user_id = (string) \Yii::$app->user->getId();
// Returns the result of ActiveQuery, and will fail if you include '->one()'
return new ActiveDataProvider([
'query' => User::find()->where(['_id' => $user_id]),
]);
}
Do it this way if you want to use ->one().
public function actionIndex()
{
$user_id = (string) \Yii::$app->user->getId();
// Returns just one record. Take note of '->one()'
return User::find()->where(['_id' => $user_id])->one();
}
Wednesday, July 7, 2021
PHP Class does not exist
While putting in place a new class, and you are sure that your namespace is correct and yet you are still getting this error, verify that the file common\components\FakeStorage.php exists.
Class common\\components\\FakeStorage does not exist
... verify that the file common\components\FakeStorage.php exists. What that means is that your class name and filename has to be the same.
Subscribe to:
Posts (Atom)