0

I have a table Company and one of it's field is status. It may have one of two values: open or closed.

I've generated a Model using Gii.

How can i filter data in a model so that I'll see only companies with "closed" status on my r=company/index page?

Usually i'd write a query like "SELECT * FROM company WHERE status = 'closed'" but i don't know where to write it in Yii 2. All these MVC stuff makes me mad. Can't understand anything, really.

Sohel Ahmed Mesaniya
  • 3,013
  • 1
  • 19
  • 29
Marat
  • 566
  • 6
  • 19
  • 1
    There are a lot of documentation on Yii site about framework structure. Read about ActiveRecord http://www.yiiframework.com/doc-2.0/guide-db-active-record.html – Anton M. Oct 06 '15 at 17:15
  • you can also build an `ActiveQuery` for it so you can do : `Company::find()->closed()->all();` (check this: http://stackoverflow.com/questions/31948917/yii2-activequery-example-and-what-is-the-reason-to-generate-activequery-class/31950149#31950149) – Salem Ouerdani Oct 07 '15 at 09:12

1 Answers1

0

You can try this:

You can filter the model search results with the following code in your controller:

$companies = Company::find()
        ->where(['status' => 'closed'])
        ->all();
waza-ari
  • 928
  • 1
  • 13
  • 34
Amin Shafiee
  • 366
  • 1
  • 6
  • 19