Basic Filtering

Hello All,

Newbie here and I am looking for some basic guidance on how to filter a table. As I am getting to understand yii I am finding that more complexed issues have very easy solutions, as I am sure is the case here. I have 1 table called clients and a client can be active, prospect, or a cold lead. I have 3 different model and controllers for the table as well as 3 different menu items. My guess is that there is a way to filter the records in the different models, but I am not sure how to do this. I was going to write a SQL statement to do it, but since many thing in yii are simple I decided to look into the filtering of the records.

So I have a couple of questions to ask, first is filtering the best way to handle the records and if so, what is the approach I should take to archive it?

Thank you in advance for your time and guidance.

Michael

First of all, make sure you’re really need these three(!) models. Common practice is one table = one model (except some twisted cases).

Next, consider using named scopes for filtering clients by type. In your case it might look like this:


$active_clients = Client::model()->active()->findAll();

If you really need to use more than one model per table, consider creating some "base" model and extending its class.

PS. Actually, I suppose I’ve not understood the question, so maybe you can elaborate.

This looks like partitioning by inheritance in SQL, where you have very similar tables. You put all data into one, create different, updatable views with a simple condition or tables that inherit from the main one.

But you don’t have to do that, you can use named scopes just as ORey suggested.

As for filtering it depends on how complex you want the search form to be and how many features should it have. I could give three examples:

  • quick search using one text box

  • search form embedded in table header that allows searching in each displayed attribute

  • a full blown search form with some complex fields not mapping to any attribute

You can use your model in the ‘search’ scenario, assign values to it’s attributes and use the search() method to create a dataProvider with some criteria set. That dataProvider fuels your grid view (aka table).

Thank you guys for replying to this post and I want to say both of you are correct on understanding what it is that I want to accomplish. I read the suggested information about name scopes and I tried playing with them a little without any luck. I inserted the below code into my model but I am unsure how to use it to filter.

I have 1 table "Clients" and I have 3 menu items all using the same table, "Clients, Prospects, Cold Leads". I would like them all to have their own forms and as nineinchnick suggested have only 1 model, but I am unsure how to set it up. Do I call the name scope from each form or add something into the controller?

Thank you for any additional guidance you can provide.

Michael







        public function scopes()

        {

            return array(

                'active_client'=>array(

                    'condition'=>'type=Active',

                ),

                'prospect_client'=>array(

                    'condition'=>'type=Prospect',

                ),

                'cold_client'=>array(

                    'condition'=>'type=Trader',

                ),

            );

        }



Scopes looks fine, you just have to use them now.

You apply them before find* methods, like so:




$prospects = Client::model()->prospect_client()->findAll();