Crud With Table Relation And Grid View

I have a two tables called category and sub_category

category


id

category

status

category


id

category_id

subcategory

status

I have created the model and CRUD using GII, now I want to display the category name on the subcategory crud admin page, could any help me on this ,

in models/subCategory.php




 public function relations() {

        return array(

            'category_id' => array(self::BELONGS_TO,'Category', 'id'),        );

    }

public function rules() {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            array('sub_category, status, created_date, category_id AS category_search, updated_date, updated_by', 'required'),

            array('category_id', 'numerical', 'integerOnly' => true),

            array('updated_by', 'numerical', 'integerOnly' => true),

            array('sub_category, status', 'length', 'max' => 255),

            // The following rule is used by search().

            // Please remove those attributes that should not be searched.

            array('id,category_id, sub_category, status, category_search', 'safe', 'on' => 'search'),

        );

    }

    public function search() {

        // Warning: Please modify the following code to remove attributes that

        // should not be searched.


        $criteria = new CDbCriteria;

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

        $criteria->together = true;

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

        $criteria->compare('category_id.id', $this->category_search, true );

        $criteria->compare('sub_category', $this->sub_category, true);

        $criteria->compare('status', $this->status, true);

       return new CActiveDataProvider($this, array(

            'criteria' => $criteria,

            'sort'=>array(

                'attributes'=>array(

                    'category_search'=>array(

                        'asc'=>'category_id.category ASC',

                        'desc'=>'category_id.category DESC',

                    ),

                    '*',

                ),

            ),

        ));



in the view/subcategory/admin.php




$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'sub-category-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array (

		'id',

                 array(

                    'name'=>'category_search',

                    'header'=>'Category',

                    'sortable'=>false,

                    'value'=>$data->category_id->category

                  ),

		'sub_category',

		'status',

		array(

			'class'=>'CButtonColumn',

		),

	),

));



Can some one help me on this issue ?

Hi psbharathy, welcome to the forum.

Did you use gii to generate the relation? Or did you manually write it?

I guess the relation should be something like this:




public function relations() {

    return array(

        'category' => array(self::BELONGS_TO,'Category', 'category_id'),

    );

}



Also you can write a query on admin.php page

for e.g…


'value'=>'GxHtml::valueEx($data->category,\'category_name\')',

or


'value'=>'GxHtml::listDataEx(subcategory::model()->find('category_id=\'1\''), 'id', 'cat_name')',

Hi softark,

Thank you for your help ,

I didn’t use gii for relation , I have added manually , also I have tried below code but didn’t work




 return array(

        'category' => array(self::BELONGS_TO,'Category', 'category_id'),

    );



Thank Ankit Modi

I have tried both codes but it doesn’t work, could you check both files and help me on this ?

Hi psbharathy,

The relation should be:




public function relations() {

    return array(

        'category' => array(self::BELONGS_TO, 'Category', 'category_id'),

    );

}



And the search method should be:




public function search() {

    $criteria = new CDbCriteria;

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

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

    $criteria->compare('category.category', $this->category_search, true );

    $criteria->compare('sub_category', $this->sub_category, true);

    $criteria->compare('status', $this->status, true);

    $criteria->compare('created_date', $this->created_date, true);

    $criteria->compare('updated_date', $this->updated_date, true);

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


    return new CActiveDataProvider($this, array(

        'criteria' => $criteria,

        'sort'=>array(

            'attributes'=>array(

                'category_search'=>array(

                    'asc'=>'category.category ASC',

                    'desc'=>'category.category DESC',

                ),

                '*',

            ),

        ),

    ));

}



BTW, I don’t like the naming of the relation … IMO, it should be ‘category’ instead of ‘category_id’.

You can not use ‘category_id’ for the relation name, because you already have ‘category_id’ as an column-based attribute.

Hi softark , Thanks for the help its working now but the second continuous row on the grid column category name didn’t show on the grid , please check the attached screen shot , check the row three and four they have category related with women and men , but it didn’t populated on the grid ?

Would you please show your current view code?

Just the relevant part in code markup, please. We don’t want to download an attached file.




$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'category-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

        'itemsCssClass'=>'table table-bordered table-primary',

	'columns'=>array(

		'id',

		array(

                    'name'=>'category',

                    'header'=>'Category',

                    'value'=>'$data->category->category'

                  ),

		'status',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); 



Thank you for the reply

I think the view code is fine.

Probably you have bad data in your table.

Try and see ‘category_id’ values.




$this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'category-grid',

        'dataProvider'=>$model->search(),

        'filter'=>$model,

        'itemsCssClass'=>'table table-bordered table-primary',

        'columns'=>array(

                'id',

                'category_id',

                /*

                array(

                    'name'=>'category',

                    'header'=>'Category',

                    'value'=>'$data->category->category'

                ),

                */

                'status',

                array(

                        'class'=>'CButtonColumn',

                ),

        ),

)); 



Thanks softark,

I have checked the subcategory table and it saved properly , please check the table entry




INSERT INTO `sub_category` (`id`, `category_id`, `sub_category`, `status`, `created_date`, `updated_date`, `updated_by`) VALUES

(1, 1, 'Kids', 'Active', '0000-00-00 00:00:00', '2013-09-26 12:27:00:00', 0),

(2, 1, 'Men', 'Active', '2013-09-26 11:29:18', '2013-09-26 11:29:18', 1),

(3, 2, 'Fashion', 'Active', '2013-10-08 12:27:34', '2013-10-08 12:27:34', 1),

(4, 1, 'Sports', 'Active', '2013-10-08 12:32:05', '2013-10-08 12:32:05', 1);



I once wrote:

It seems like you have a wrong definition of the relation … like:




public function relations() {

    return array(

        'category' => array(self::BELONGS_TO, 'Category', 'id'),

    );

}



Regards

P Subramania Bharathy

Project Manager

Ragaventera Designs