Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

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

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 following user(s) said Thank You: admin, organicwebs, krasy

Please Log in or Create an account to join the conversation.

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

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

Please Log in or Create an account to join the conversation.

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
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

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.053 seconds

I jumped and started to work on a demo component... but 2 days later this demo component became the real component. I just showed today the end result to my customer and he turned to me and said... "this is more than I expected"... All of this is because Cook did cut about 70% of my work and provided me more ways to improve the usability of the component. The end result was 17 tables all related between than to generate a full dashboard for the travel agents. Thanks for Cook developers for such great tool. This component would not be possible to be done at short time with all the features in it
Griiettner (Forum)  

Get Started