Welcome, Guest
Username: Password: Remember me

TOPIC: Tip when forking a mulitlayout model

Tip when forking a mulitlayout model 13 Apr 2016 11:53 #13910

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
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 :)
The administrator has disabled public write access.
The following user(s) said Thank You: admin, organicwebs, krasy

Tip when forking a mulitlayout model 13 Apr 2016 14:41 #13911

  • krasy
  • krasy's Avatar
  • Offline
  • Premium Member
  • Posts: 90
  • Thank you received: 8
  • Karma: 5
thx for sharing,
I have similar problem,
with controllers
where are redirection of all buttons,
(in my app nearly all links carry parameters for filters for collectins or fk's for forms)

if I add new page/view and have controller forked earlier,
I have to merge old controller with new one,
perhaps this switch will work in that situation also, thx
The administrator has disabled public write access.

Tip when forking a mulitlayout model 14 Apr 2016 10:25 #13912

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Thanks Romkabouter,

A good way to delegate to the default generated and fork only the strict necessary.

K++
Coding is now a piece of cake
The administrator has disabled public write access.
Time to create page: 0.107 seconds

Get Started