I am trying to build a relational query but not sure how.
I have two models, Page and Menu where a menu can have many pages but each page can only belong to one menu.
I have set-up the relationships as shown
class Menu
public function relations()
{
return array(
'page'=>array(self::HAS_MANY, 'Page', 'pageId', 'order'=>'??.listOrder'),
);
}
class Page
public function relations()
{
return array(
'menu'=>array(self::BELONGS_TO, 'Menu', 'menuid',
'joinType'=>'INNER JOIN'),
);
}
The Page table has a column menu (int) that relates to menu.id of the menu table. I need to write a query that returns pages where page.status='1' and menu.name is of a certain string.
I have tried this but are getting errors. I have rewritten some of the relations as I noticed that the mysql code for the Yii-blog was a little wrong in the relations() method from which I was copying.
Therefore I am left with this.
class Menu
public function relations()
{
return array(
'page'=>array(self::HAS_MANY, 'Page', 'id', 'order'=>'??.listOrder'),
);
}
class Page
public function relations()
{
return array(
'menu'=>array(self::BELONGS_TO, 'Menu', 'id',
'joinType'=>'INNER JOIN'),
);
}
Therefore in sql Page.menu=Menu.id but I want to filter on Menu.alias='main_menu' for example.
I have modified your example to suit as follows:
$pages=Page::model()->with('menu')->findAll('Page.status=1 AND ??.alias=:alias', array(':alias'=>$this->menuAlias));
This however is returning an error that states the error "near '??.alias='main_menu')'"
Should the framework be replacing ??. with the table name? I tried putting in Menu.alias in it's place but it returned an error stating that the column did not exist. I guess I'm almost there but missing something.