Answer the question
In order to leave comments, you need to log in
How to create a condition in yii2?
How to create a condition, if, for example, there are less than 10 products, then the required number of products with certain values is created?
public function actionCreate()
{
$model = new Product();
if(<тут должно быть условие>){
$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();
}
}
Answer the question
In order to leave comments, you need to log in
make a method that checks the count:
$model2 = Product::find()->count();
then if($model2 < 10 ){
for($i=0; $i<$model2; $i++)
{
// your code is here
}
}
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++;
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question