Hi all,
I wanted to share a little tip when in some case you have multiple layouts on a table and need to fork one of the models behind it.
For instance if you need extra data from Joomla tables, you can fork the model to aquire that data.
When forking the model, you should copy the prepareQuery into your fork file.
But when you have multiple layouts, the query for every layout is also in that function and you need to copy the whole function.
This is not a problem, until you deside to change a layout. Then you also have to copy the whole funtion again.
A simple trick what I use is:
protected function prepareQuery(&$query) {
switch ($this->getState('context', 'all'))
{
case 'yourview.layout1': $this->prepareQueryLayout1($query); break;
default: parent::prepareQuery($query); break;
}
}
This way you can override certain contexts (layouts), but still keep everyting else default.
Offcourse you need to create the functions in the forked model also:
private function prepareQueryLayout1(&$query) {
$acl = YourComponentHelper::getActions();
//FROM : Main table
$query->from('#__yourcomponent_yourtable AS a');
//IMPORTANT REQUIRED FIELDS
$this->addSelect( 'a.id');
//BASE FIELDS
$this->addSelect( 'a.yourcolumn');
$this->addSelect( '_users_.email');
//JOIN
$this->addJoin('`#__users` AS _users_ ON _user_.id = a.joomlauserid', 'LEFT');
//FILTER - Access for : Root table
//WHERE - FILTER : Show
// Apply all SQL directives to the query
$this->applySqlStates($query);
}
Do not forget to include the filters and applySqlStates at the end!
Happy coding