Note that there are some explanatory texts on larger screens.

plurals
  1. POIs my use of MVC overcomplicating things and cuasing duplicate code?
    primarykey
    data
    text
    <p>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?</p> <p>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?</p> <p>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?</p> <p>Sample code from controller</p> <pre><code>// processes the new site data public function add_new_process() { // execute action in model $Model_Websites = new Model_Websites(); $name = $this-&gt;request-&gt;getPropertyFiltered('sitename',array('sanitize')); $descrip = $this-&gt;request-&gt;getPropertyFiltered('descrip',array('sanitize')); $url = $this-&gt;request-&gt;getPropertyFiltered('siteurl',array('sanitize')); $signup_url = $this-&gt;request-&gt;getPropertyFiltered('signupurl',array('sanitize')); $acct_id = $this-&gt;request-&gt;getPropertyFiltered('acct_id',array('sanitize')); $thumbnail = $this-&gt;request-&gt;getPropertyFiltered('thumb',array('sanitize')); if($Model_Websites-&gt;addNewSite($name,$descrip,$url,$signup_url,$acct_id,$thumbnail)) { $this-&gt;request-&gt;addFeedback("Your new website has been added succesfully!"); $this-&gt;request-&gt;setFeedbackStatus(true); $this-&gt;request-&gt;storeFeedbackInSession(); $this-&gt;template-&gt;redirectBrowser(__SITE_URL.'/websites/'); } else { $this-&gt;template-&gt;setProperty('page_title', Registry::getConfig('site_name').' :: Add New Website' ); $this-&gt;template-&gt;render('websites','show_form'); // controller,view } } </code></pre> <p>Sample code from Model</p> <pre><code>function addNewSite($name,$descrip,$url,$signup_url,$acct_id,$thumbnail) { $pdo = ConnectionFactory::instance()-&gt;getConnection(); $stmt = $pdo-&gt;prepare("INSERT INTO {$this-&gt;db_table_websites} SET name = :name , descrip = :descrip , url = :url , signup_url = :signup_url , acct_id = :ccbill_site_id , thumbnail = :thumbnail "); $stmt-&gt;bindParam(':name', $name, PDO::PARAM_STR); $stmt-&gt;bindParam(':descrip', $descrip, PDO::PARAM_STR); $stmt-&gt;bindParam(':url', $url, PDO::PARAM_STR); $stmt-&gt;bindParam(':signup_url', $signup_url, PDO::PARAM_STR); $stmt-&gt;bindParam(':acct_id', $acct_id, PDO::PARAM_STR); $stmt-&gt;bindParam(':thumbnail', $thumbnail, PDO::PARAM_STR); if($stmt-&gt;execute()) return true; else return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload