Relation Between 2 Tables

Hi,

This may be kind of duplicate but I’ve been digging into other posts and just can’t get to the bottom of it. Sorry for that.

I’m sure the problem is just my noobness with Yii, I’m just starting :unsure:

I have two tables:

  • category

  • category_description

category_description is populated with the category title, description, etc (in different languages).

Tables


CREATE TABLE arc_category (

	id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,

	parent_id int(11) NOT NULL DEFAULT '0',

	top INTEGER NOT NULL,

	image VARCHAR(128) DEFAULT NULL,

	sort_order INTEGER NOT NULL DEFAULT '0',

	status INTEGER NOT NULL,

	create_time INTEGER,

	update_time INTEGER

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;


CREATE TABLE arc_category_description

(

	id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,

	category_id INTEGER NOT NULL,

	language_id INTEGER NOT NULL,

	title VARCHAR(64) NOT NULL,

	meta_title TINYTEXT NOT NULL,

	description TEXT NOT NULL,

	meta_description TINYTEXT NOT NULL,

	seo_url VARCHAR(128) NOT NULL,

	CONSTRAINT FK_category_description FOREIGN KEY (category_id)

		REFERENCES arc_category (id) ON DELETE CASCADE ON UPDATE RESTRICT

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Models


class Category extends CActiveRecord

{

...

	public function relations()

	{

		return array(

			'categoryDescriptions' => array(self::HAS_MANY, 'CategoryDescription', 'category_id'),

		);

	}

...

}


class CategoryDescription extends CActiveRecord

{

...

	public function relations()

	{

		return array(

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

		);

	}

...

}

View


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

    'dataProvider' => $dataProvider,

    'columns' => array(

        'id',

        'categoryDescriptions.title',

        'top',

        'status',

    ),

));

My problem is ‘categoryDescriptions.title’ is not returning the category name (the record exists in the database).

From what I understand this should be the most clean and easy way to print the grid with all categories.

What am I doing wrong? Thank you all!

Hi

there are many categoryDescriptions (HAS_MANY)

So you cannot set directly categoryDescriptions.title,

instead of that try this


...

array(

'header'=>'category Description',

'value'=>'($data->categoryDescriptions) ? $data->categoryDescriptions[0].title : "-"',

'type'=>'raw'

),

...

Hi crush,

In your model, you have defined a category has many category_descriptions. Using ‘categoryDescriptions.title’ in your view will not work because an array of all the category_descriptions for that category will be returned.

You’ll need a loop to get the titles of the categoryDescriptions that belong to that category.

Hi,

Thank you for your help and thoughts and thanks for making me feel the Yii community.

KonApaz


'value'=>'($data->categoryDescriptions) ? $data->categoryDescriptions[0].title : "-"',

did the trick … But only for the first [0] result in the list (the others categories return "-" from your if/else condition).

BlkRaven

You are right.

Your two opinions just made me realize i was trying a wrong approach.

HAS MANY will not work with ‘categoryDescriptions.title’ (HAS MANY - will create an array of the many results. Will need a loop).

I just need to return a list of categories with the category ‘title’ according to the default set ‘language’ (one result for every category).

basically - Return all categories on categoryDescription.language = i

Thank you guys. Will post my progress and solution for this.

Any suggestions on the best approach to achieve it will be much appreciated.

Thanks!

Hi again

$data in CGridView is the current record of dataprovider (for each line) so if the other records except the first one return description means that the other has no description!

If you want the each line to display all the descriptions the it is better to make a function in the controller that accept the parameter $data and return a concatenate all of the descriptions

Hi crush,

You can refer to the guide http://www.yiiframework.com/doc/guide/1.1/en/database.ar#named-scopes, see the Default Scope section, you will get the idea.

check this artical

And here’s wiki article where described how to render complex data in your grid-view.