Answer the question
In order to leave comments, you need to log in
Why doesn't save() work in Yii2?
There is a code like this:
$model=new Products();
$model=$model->findOne(['id'=>1]);
$model->name="Новое название";
$model->save();
class Products extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'products';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'url', 'price','quantity','category_id','options_id','mark','model'], 'required'],
[['price'], 'number'],
[['full_desc'], 'string'],
[['category_id','options_id','quantity','status'], 'integer'],
[['name', 'url'], 'string', 'max' => 50],
[['short_desc'], 'string', 'max' => 250]
];
}
}
Answer the question
In order to leave comments, you need to log in
$model = Product::findOne(['id'=>1]);
$model->name="Новое название";
$model->save();
Very confusing $model=$model->findOne(['id'=>1]). Probably worth doing this:
$oldmodel=Product::findOne(['id'=>1]);
$model=new Product();
$model->attributes = $oldmodel->attributes;
$model->name="Новое название";
$model->save();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question