Yii Framework Forum: Crud With Table Relation And Grid View - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Crud With Table Relation And Grid View Rate Topic: ****- 1 Votes

#1 User is offline   psbharathy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 17-August 13
  • Location:coimbatore

Posted 28 September 2013 - 05:17 AM

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 ?
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 28 September 2013 - 09:02 AM

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'),
    );
}

0

#3 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 856
  • Joined: 19-February 13
  • Location:India

Posted 29 September 2013 - 11:11 PM

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')',

Thanks,
Ankit Modi
Skype : amodi06
0

#4 User is offline   psbharathy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 17-August 13
  • Location:coimbatore

Posted 30 September 2013 - 01:30 AM

View Postsoftark, on 28 September 2013 - 09:02 AM, said:

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'),
    );
}



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'),
    );

0

#5 User is offline   psbharathy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 17-August 13
  • Location:coimbatore

Posted 30 September 2013 - 01:38 AM

View PostMaggie Q(Ankit Modi), on 29 September 2013 - 11:11 PM, said:

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')',



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

Attached File(s)


0

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 05 October 2013 - 08:35 AM

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.
0

#7 User is offline   psbharathy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 17-August 13
  • Location:coimbatore

Posted 08 October 2013 - 05:35 AM

View Postsoftark, on 05 October 2013 - 08:35 AM, said:

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 ?

Attached File(s)


0

#8 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 08 October 2013 - 06:09 AM

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.
0

#9 User is offline   psbharathy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 17-August 13
  • Location:coimbatore

Posted 08 October 2013 - 06:21 AM

View Postsoftark, on 08 October 2013 - 06:09 AM, said:

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
0

#10 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 08 October 2013 - 05:02 PM

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',
                ),
        ),
)); 

0

#11 User is offline   psbharathy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 17-August 13
  • Location:coimbatore

Posted 09 October 2013 - 02:07 AM

View Postsoftark, on 08 October 2013 - 05:02 PM, said:

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

0

#12 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 09 October 2013 - 04:21 AM

I once wrote:

View Postsoftark, on 05 October 2013 - 08:35 AM, said:

The relation should be:
public function relations() {
    return array(
        'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
    );
}



It seems like you have a wrong definition of the relation ... like:
public function relations() {
    return array(
        'category' => array(self::BELONGS_TO, 'Category', 'id'),
    );
}

0

#13 User is offline   psbharathy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 17-August 13
  • Location:coimbatore

Posted 09 October 2013 - 04:30 AM

Quote

Awesome! Now its working great ! Thanks for your continuous support.
"No Other technical Forums and MVC framework won't HELP like Yii , Yii is Rocking... ".


Regards
P Subramania Bharathy
Project Manager
Ragaventera Designs
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users