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.090 seconds

I'm playing around with the new mvc and the FORK feature is FANTASTIC!!! it's saving me a lot of time! you are doing a very good job!!

Tomaselli (Forum)  

Get Started