How to create a condition in yii2?

0 like 0 dislike
41 views
Like new condition, if for example the goods is less than 10, then creates the required number of goods c particular values ?
public function actionCreate() { $model = new Product(); if(<there's gotta be some condition>){ $model->category_id='2'; $model->author_id=$app->user->identity['id']; $model->name='name'; $model->content='content'; $model->price='0'; $model->keywords='NULL'; $model->description='NULL'; $model->hit='0'; $model->new='0'; return $model->save(); } }

5ad5f0c9a84f1298916431.png
by | 41 views

2 Answers

0 like 0 dislike
make a method that checks the number:
$model2 = Product::find()->count();

then if($model2 < 10 ){
for($i=0; $i<$model2; $i++)
{
// your code here
}

}
by
0 like 0 dislike
public function actionCreate() { $product_count = Product::find()->count(); while ($product_count < 10) { $model = new Product; $model->category_id='2'; $model->author_id=$app->user->identity['id']; $model->name='name'; $model->content='content'; $model->price='0'; $model->keywords='NULL'; $model->description='NULL'; $model->hit='0'; $model->new='0'; if ($model->save()) { $product_count++; } } }
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
asked Apr 3, 2019 by VladimirMelnik
0 like 0 dislike
1 answer
asked Apr 9, 2019 by t-alexashka
0 like 0 dislike
1 answer
asked Jun 10, 2019 by Chernichko
0 like 0 dislike
2 answers
asked Jun 8, 2019 by criminalist
110,608 questions
257,186 answers
0 comments
33,896 users