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
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!