A little confused about MVC and where to put a database query
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 delet开发者_StackOverflow社区e
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?
I would try to keep all database functionality in your model. If you don't know which model a method should go in, it's possible that you need to change your models to better reflect your problem.
In your case, though, I think this method would go in allapps since it can handle operations on multiple apps.
If you pass in a task, that method will be called. If you want to then call the display method, just call it at the end of your edit method.
When in doubt, take a look at the weblinks component's models and controllers. They are very simple and a good example of how to do MVC in Joomla!.
精彩评论