How To Deal With Many And Search

I am getting into the more complex functions for user interfaces. I have a request to search a list of items that belong to categories.

It’s kind of like a shopping cart, I want to filter a list then put the items in a list for the client / customer.

Item can belong to many categories

Item (id, name)

Category (id, name)

Item_Category (item_id, cat_id)

In the CGridView I would like to open the "search" form and select from the list of categories.

The CGridView model will be Items that are filtered that belong to the categories chosen in the multiselect list.

Do I need to use Item::model()->with(item_category)->findAll() to assign to my model?

I don’t have much coded yet…was starting with the GII generated forms for Item (admin).

Any suggestions on how to utilize the relation?

In my Items model I have a custom searchItems() function as follows:




   

public function searchItems() {

        $criteria = new CDbCriteria;


        $criteria->compare('active', $this->active);

        $criteria->compare('status_id', $this->status_id);

        $criteria->compare('quality_id', $this->quality_id);

        $criteria->compare('category_id', $this->category_id);

             

        return new CActiveDataProvider(get_class($this), array(

 		'pagination'=>array(

        	'pageSize'=> Yii::app()->user->getState('pageSize',User::model()->myAccount()->find()->pagesize),

    		    ), 

                'criteria' => $criteria,

                ));

    }



Is this where I can integrate the ItemCategory Model?

I read about mergeWith and with() methods…do they apply here?

In the _search form I added a field for category_id and a drop down list.




 array(

                'name'   => 'category_id',

                'value'  => 'isset($data->category->category)?$data->category->category:"N/A"',

                'filter' => CHtml::listData(Category::model()->findAll(),'id','category'),

                ),



I added the "with" clause in the "search" function in the model, but no luck…generating errors about column name being abiguous…which it is as "category_id" exists in Item_Category table and Category




        $criteria = new CDbCriteria;


        $criteria->with = array('itemCategories');

        $criteria->compare('category_id', $this->category_id);  // want this to match itemCategories.category_id


        $criteria->compare('active', $this->active);

        $criteria->compare('status_id', $this->status_id);

        $criteria->compare('quality_id', $this->quality_id);

     



If I add $criteria->compare(‘itemCategories.category_id’, $this->category_id);

It errors out with invalid column reference in where clause…"itemCategories.category_id".

I found this in a few topics and it worked for me.

$criteria->together = true;

I also was able to prefix the relation on to the column reference after adding this.

Adding this has also modified the array that is returned. For some reason only 2 rows are returned in the form before the

_search page is even entered…

This behaviour is not acceptable !

I understand that the loading is being affected by the outter join to the category table but this is crazy.