A Model of Controlled failure



  • More Joomla! fun today. I was further dissecting their tutorial on Developing a Model-View-Controller component when I realized that something was amiss in how data was passed from the Controller to the Model.

    Here is an example task from the Controller that is the end point of a Post event that saves the form's data to the database table:

    /**
     * save a record (and redirect to main page)
     * @return void
     */
    function save()
    {
    	$model = $this->getModel('hello');
     
    	if ($model->store()) {
    
                    ....  (code snipped out)  ....
    
    	}
    
            ....  (code snipped out)  ....
    }
    

    Hmm .. I can see where the store() method on the Model is called to save the data .. .but where is *that* form data?????

    Queue the store method in the model!

    /**
     * Method to store a record
     *
     * @access	public
     * @return	boolean	True on success
     */
    function store()
    {	
            ....  (code snipped out)  ....
    
    $data = JRequest::get( 'post' );
    
        ....  (code snipped out)  ....
    

    }

    Yep - the Model extracts the form data from the request itself! Thus binding the Model to (1) A Form, (2) A Post event. (Sadly the entire tutorial is built like this.)

    Great way to separate concerns there boys!



  •  While I agree its a wtf, if you look at any of the core joomla components its a bit more like this:


    function save(){
      
    $post = JRequest::get( 'post' );

    $model = $this->getModel('hello');
    
    if ($model->store($post)) {
    
                ....  (code snipped out)  ....
    
    }
    
        ....  (code snipped out)  ....
    

    }

     function store($post){

    do something with the post here and save to db

     

    }

     

    At the end of the day its a badly written tutorial that doesn't reflect what any of the core devs write, or for that matter anything I've written either, it is in fact brain dead to be getting the post fromt the model.  Regardless good catch, have you told them yet?



  • @roba121 said:

    At the end of the day its a badly written tutorial that doesn't reflect what any of the core devs write,

    I'd dispute you on this. This tutorial is included as part of the Joomla! site, so it is the public face of Joomla!. As such I hold the people who wrote this code to a higher standard than if it was on some Joe Blogs website.
    @roba121 said:
    it is in fact brain dead to be getting the post fromt the model
    Totally agree with you and I'd write code as you indicated as well.
    @roba121 said:
    Regardless good catch, have you told them yet?
    This is my first delving into Joomla! itself. My heads still swimming a bit with regards to doing things the Joomla way so I haven't even considered sending them a comment yet. Its also been around for 2 years so I am amazed that no one else has spotted that, commented themselves or not changed the tutorial either.



  • @roba121 said:

    At the end of the day its a badly written tutorial
    that doesn't reflect what any of the core devs write,

    @OzPeter said:

    I'd dispute you on this. This tutorial is included as part of the Joomla! site, so it is the public face of Joomla!. As such I hold the people who wrote this code to a higher standard than if it was on some Joe Blogs website.

     I just meant that the tutorial is wrong and they do not in fact do it that way in the code. So as far as actually doing something sucky they don't, of course misleading new devs into thinking this is ok, is a whole other problem. As far as it being their fault, totally right.

     I'd suggest learning on com_weblinks, it follows there core MVC pattern but is simple enough to understand and work backwards from. Good Luck!

     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.