I've wrote my own PHP MVC however i'm struggling to distinguish between the model and controller part. For example with a simple form that will add data to the database, the controller is pulling the form data from a request object, and passing it to the Model to handle the actual database insert. However there seems a lot of somewhat duplicate code in here, would it not be simpler to remove the Model part altogether and simply have the controller do the database insert?
I understand that in some cases i may have multiple controllers using the same Model action, however for the occasioanl time this happens it doesn't seem worth all the extra coding required to constantly seperate the Model and controller?
It's not so much duplicate code more that it seems a long winded way of doing things as i'm writing a lot of code for what is essentially a simple function, if that makes sense?
Sample code from controller
// processes the new site data
public function add_new_process() {
// execute action in model
$Model_Websites = new Model_Websites();
$name = $this->request->getPropertyFiltered('sitename',array('sanitize'));
$descrip = $this->request->getPropertyFiltered('descrip',array('sanitize'));
$url = $this->request->getPropertyFiltered('siteurl',array('sanitize'));
$signup_url = $this->request->getPropertyFiltered('signupurl',array('sanitize'));
$acct_id = $this->request->getPropertyFiltered('acct_id',array('sanitize'));
$thumbnail = $this->request->getPropertyFiltered('thumb',array('sanitize'));
if($Model_Websites->addNewSite($name,$descrip,$url,$signup_url,$acct_id,$thumbnail)) {
$this->request->addFeedback("Your new website has been added succesfully!");
$this->request->setFeedbackStatus(true);
$this->request->storeFeedbackInSession();
$this->template->redirectBrowser(__SITE_URL.'/websites/');
} else {
$this->template->setProperty('page_title', Registry::getConfig('site_name').' :: Add New Website' );
$this->template->render('websites','show_form'); // controller,view
}
}
Sample code from Model
function addNewSite($name,$descrip,$url,$signup_url,$acct_id,$thumbnail) {
$pdo = ConnectionFactory::instance()->getConnection();
$stmt = $pdo->prepare("INSERT INTO {$this->db_table_websites} SET
name = :name
, descrip = :descrip
, url = :url
, signup_url = :signup_url
, acct_id = :ccbill_site_id
, thumbnail = :thumbnail
");
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':descrip', $descrip, PDO::PARAM_STR);
$stmt->bindParam(':url', $url, PDO::PARAM_STR);
$stmt->bindParam(':signup_url', $signup_url, PDO::PARAM_STR);
$stmt->bindParam(':acct_id', $acct_id, PDO::PARAM_STR);
$stmt->bindParam(':thumbnail', $thumbnail, PDO::PARAM_STR);
if($stmt->execute()) return true;
else return false;
}