Hello, I’m new to Yii. In my previous experience I created multilanguage application with help of Doctrine 1.2. Now I’m trying “port” this application to Yii with it’s AR models. DB schema is as following figure:
So basicaly - I got a PAGE entity, which is in tree (nested set). PAGE has_many of PAGE_I18Ns. In i18ns are saved localized name & url of the PAGE. i18n record is also related to LANGUAGE (i18n has_one lang). Please DON’T pay attention to other tables in schema (menus, content, content_i18n)!
So what I basicaly need is to “fetch” (using eager loading approach to save SQL queries) a particular PAGE and coresponding PAGE_I18N record in particular LANGUAGE (identified by languages.code). Remember - I must “fetch” this data using eager loading approach by Yii AR models (don’t wana use third party ORM).
BTW here is a working SQL query, which fullfill my needs:
[sql]
SELECT
pages.id AS page_id,
pages_i18n.id AS pageI18N_id, pages_i18n.name, pages_i18n.url,
languages.id AS lang_id, languages.code
FROM pages
LEFT JOIN pages_i18n ON pages.id = pages_i18n.page_id
LEFT JOIN languages ON pages_i18n.language_id = languages.id
WHERE pages.id = 2 AND languages.code = ‘sk’
[/sql]
Models definitions follows:
class Pages extends CActiveRecord
{
/**
* The followings are the available columns in table 'pages':
* @var integer $id
* @var integer $menu_id
* @var integer $root_id
* @var integer $lft
* @var integer $rgt
* @var integer $level
* @var integer $visible
* @var integer $contactForm
*/
public function relations()
{
return array(
//'contents' => array(self::HAS_ONE, 'Content', 'page_id'),
//'menu' => array(self::BELONGS_TO, 'Menus', 'menu_id'),
'root' => array(self::BELONGS_TO, 'Pages', 'root_id'),
'pages' => array(self::HAS_MANY, 'Pages', 'root_id'),
'pages_i18ns' => array(self::HAS_MANY, 'PagesI18n', 'page_id'),
//'images' => array(self::MANY_MANY, 'Images', 'pages_images(page_id, image_id)'),
//'products' => array(self::MANY_MANY, 'Products', 'pages_products(page_id, product_id)'),
);
}
}
class PagesI18n extends CActiveRecord
{
/**
* The followings are the available columns in table 'pages_i18n':
* @var integer $id
* @var integer $page_id
* @var integer $language_id
* @var string $name
* @var string $url
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'language' => array(self::BELONGS_TO, 'Languages', 'language_id'),
'page' => array(self::BELONGS_TO, 'Pages', 'page_id'),
);
}
}
class Languages extends CActiveRecord
{
/**
* The followings are the available columns in table 'languages':
* @var integer $id
* @var string $code
* @var string $name
*/
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
//'content_i18ns' => array(self::HAS_MANY, 'ContentI18n', 'language_id'),
'pages_i18ns' => array(self::HAS_MANY, 'PagesI18n', 'language_id'),
//'products_i18ns' => array(self::HAS_MANY, 'ProductsI18n', 'language_id'),
);
}
}
Here is also some basic approach I tryied
$c = new CDbCriteria;
$c->addCondition('visible=true');
$c->addCondition('contactForm=false');
$c->addCondition('level!=0');
$c->with = 'pages_i18ns';
$c->order = 'id ASC';
$c->limit = 1;
$result = Pages::model()->find($c);
Half of a kindom for working solution.
THX.