A little confused about MVC and where to put a database query

Posted by jax on Stack Overflow See other posts from Stack Overflow or by jax
Published on 2010-06-17T05:49:21Z Indexed on 2010/06/17 5:53 UTC
Read the original article Hit count: 180

Filed under:

OK, so my Joomla app is in MVC format. I am still a little confused about where to put certain operations, in the Controller or in the Model. This function below is in the controller, it gets called when &task=remove. Should the database stuff be in the Model? It does not seem to fit there because I have two models editapp (display a single application) and allapps (display all the applications), now which one would I put the delete operation in?

    /**
 * Delete an application
 */
function remove() {
    global $mainframe;

    $cid = JRequest::getVar( 'cid', array(), '', 'array' );
    $db =& JFactory::getDBO();

    //if there are items to delete
    if(count($cid)){
        $cids = implode( ',', $cid );
        $query = "DELETE FROM #__myapp_apps WHERE id IN ( $cids )";
        $db->setQuery( $query );

        if (!$db->query()){
            echo "<script> alert('".$db->getErrorMsg()."');window.history.go(-1); </script>\n";
        }
    }

    $mainframe->redirect( 'index.php?option=' . $option . '&c=apps');
}

I am also confused about how the flow works. For example, there is a display() function in the controller that gets called by default. If I pass a task, does the display() function still run or does it go directly to the function name passed by $task?

© Stack Overflow or respective owner

Related posts about joomla