Welcome Guest, Not a member yet? Register   Sign In
MVC Question about site wide feature
#1

[eluser]SethG[/eluser]
Have an MVC question for you.

Here's my scenario. I have a feature of an application called notes. I have a controller for notes and a model for notes. I have tried to build out notes as a feature that can be used related to multiple objects in the application and therefore would cross multiple controllers. For example I am creating notes for projects, notes for organizations, notes for people, ....

So this leads to my question, I want to be able to display a list of notes and to do so by calling a simple function with parameters that explain what notes to display (for example passing an organization id). The function will return the html code to display the results. Currently I have done this in the model. I have a model function that not only queries for the data but generates and returns the html for display. Is this the wrong approach? Should I create a notes helper which takes the query array and generates the html?

Hopefully this won't confuse but here is the code for what I have in the model ... again my question is should the code for formatting the results of a query for a feature such as this be better organized in a helper (or something else) instead of in the model?
Code:
function getNotesList($parentType = '', $parentID = '', $limit = '') {
    
    /* First generate and execute query */
    
        $this->db->orderby('notes_created desc, notes_id desc');
        $this->db->limit($limit);
        if ($parentID) $this->db->where('notes_parent_id', $parentID);
        if ($parentType) $this->db->where('notes_parent_type', $parentType);
        
        switch ($parentType) {
        case 1:
          $this->db->join('org', 'org.org_id = notes.notes_parent_id');
          break;
        case 2:
          $this->db->join('contacts', 'contacts.ID = notes.notes_parent_id');
          break;
        case 3:
          $this->db->join('jobs', 'jobs.job_id = notes.notes_parent_id');
          break;
        case 4:
          $this->db->join('notes', 'notes.notes_id = notes.notes_parent_id');
          break;
        case 5:
          $this->db->join('invoice', 'invoice.invoice_id = notes.notes_parent_id');
          break;  
        }
  
        $query = $this->db->get('notes');
    
    /* Then if there are results format and return results for display */
        
        if ($query->num_rows() > 0) {
            $output = '<h2>Notes</h2><div id="notes">';
            foreach ($query->result() as $note) {
                $output .= '<div class="item">';
                $output .= '<h3>'.anchor('notes/detail/'.$note->notes_id, 'Note by '.$note->posted_by.' on '.dateFormat($note->notes_created).'' ).'</h3>';
                $output .= nl2br($note->notes);
                $output .= '</div>';    
            }
            
            $output .= '</div>';
                
         } else {
             $output = '<h2>Notes</h2><div id="notes"><h3>There are currently no notes to display</h3></div>';
         }
        
         return $output;
    }
#2

[eluser]wiredesignz[/eluser]
It's generally bad practice to have HTML created in your model, it would be better to create a partial View for that HTML snippet.
#3

[eluser]SethG[/eluser]
[quote author="wiredesignz" date="1200190852"]It's generally bad practice to have HTML created in your model, it would be better to create a partial View for that HTML snippet.[/quote]

That makes perfect sense ... I think. It seems so simple I wonder why I missed that ... but that definitely happens. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB