Skip to main content

Joomla! 2.5: Table sorting / The MVC problem.

Well, I feel kind of embarrassed that I need to write about this topic... I always thought I kind of understood MVC architecture. But this one drove me nuts:

I am working on a custom article management system for my front end users. I know that there might be (for sure) extensions to accomplish this, but you know how it is... you want to do it yourself. Everything worked out fine, until I got to the point of sorting the article table (alphabetically, chronologically etc.). This, too, could be done with extensions or existing jquery / other techniques. Could be.

Well, I decided to do it with the traditional Javascript / Ajax / PHP approach. Jacvascript function triggers AJAX request, AJAX request goes to controller, controller queries the database and creates an array that can be handled by the view.

So far so good. But how do I get the view to reload without refreshing the entire page (which would obviously loose the sort order)? I made it as far as updating the model, but I couldn't manage an update of the view - yes, it reloaded, but the array with the data for the table seemed to be empty, even though I set it. I was about to do copy and paste... but I am very reluctant to redundancy.

Eventually, my controller calls a custom view method and passes the array (without using the model), then the controller calls a custom display method. And voilà, it works.

The code:

controller.php
                ...
        $db->setQuery($query);
        $result = $db->loadObjectList();

        $view = $this->getView(<viewname>, 'html');
        $view->setMyArticles($result);
        $view->mydisplay();


If your view class name is "MyviewViewSpecialname", then viewname would be "specialname" (lowercase). This is important if you have multiple views.

view.html.php
     // Overwriting JView display method
     function display($tpl = null)
        {
             // Assign data to the view
             $this->msg = $this->get('Msg');

             $this->myarticles = $this->get('MyArticles');
                     
              // Check for errors.
              if (count($errors = $this->get('Errors')))
              {

              JError::raiseError(500, implode('<br />', $errors));
              return false;
        }
        // Display the view
        parent::display($tpl);
    }

    public function mydisplay($tpl = null){
        parent::display($tpl);
    }
    public function setMyArticles($array){
        $this->myarticles = $array;
    }


This is it! This cost me about 20 hours to figure it out... Hope it can help you, too!

Comments

Popular posts from this blog

Joomla! 2.5: Multiple views, one model

Here goes MVC again: I was a bit confused that every time I created a second or third view for a component, I had to create a separate model for it. Did not make sense to me - I had the necessary algorithms in the model for the first view, did I really need to do copy & paste? I hate it! Redundancy, redundancy, redundancy. That case occurred in my current project - I created user profiles and needed an "edit" view. The data is the same, but I need to have the data in editable text fields instead of fixed labels, and maybe some other buttons here and there...  Well, some time later I realized that I just did not quite understand what Joomla! acutally means by "view".  And that there is something called "layout". Long story short: If you have a model that provides all necessary information and you just want to have a different presentation of it, just add a new .php file to your views/<yourviewname>/tmpl folder - let's call it "layo

Eureka! Intel N 7260 Issue Appears To Be Solved

For a while now I've been having annoying trouble with my wireless adapter (Intel N 7260 - yes, I know it is known to cause trouble...). Not only did it not connect properly, it broke the wifi and the internet connection of the router in a way that no other device in the network could access the internet any more. I might just have discovered another possible cause: And guess what, Windows took me there. I know, right? So, this is what I did (I am on Win 10 right now): - It did not connect properly, as usual (for me this only happens when at home, where I am using a Speedport W 504V router) - so I ran Windows "troubleshooting" (right-click on the wifi icon in the task bar to get there) - It told me that there was an issue with a network protocol missing on my machine (which is rather ridiculous) - BUT at the bottom of the window it offered me a link to detailed information about the issue, which took me to a quite detailed troubleshooting report - This report lists

Galaxy S5 Lollipop Update Issue

Disclaimer: getting anything wrong with flashing might brick your device - this is not a manual. I am not responsible for any damage to your phone or data...  -- So I finally checked the software update availability on my Samsung Galaxy S5, and guess what - the Android 5.0 update was available. Finally. I cleared up some space on my phone (it claims that it needs 3 GB of free space for the installation) and started the update. It is a big image (1.4 GB), so it takes a while to download, then the phone shuts down, restarts, and starts to install. I started wondering when the installation process started at 25%, went down to 1, up to 20, 30% and then - "error". Phone restarts, everything OK except for the fact that I still have KitKat on my phone. Tried again. Same result. Tried again. Same result. Googled it. No results. Nobody seemed to run into that same error. There was indeed an error message after the phone restarted successfully - "Try it again, try Kies or go