A
A
Ainur1232018-05-03 17:36:51
Yii
Ainur123, 2018-05-03 17:36:51

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();
        }
}

5ad5f0c9a84f1298916431.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex McOwkin, 2018-05-03
@phpist85

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
}
}

I
Ilya, 2018-05-03
Hrebet @hrebet

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 question

Ask a Question

731 491 924 answers to any question