CodeIgniter Forums
Fat models - skin controllers with File Uploading - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Best Practices (https://forum.codeigniter.com/forum-12.html)
+--- Thread: Fat models - skin controllers with File Uploading (/thread-574.html)



Fat models - skin controllers with File Uploading - Lykos22 - 12-22-2014

Hi all, I'd like to ask a question if possible regarding the "fat models - skin controllers" best practices.

I have inside my model a method that holds both file uploading and image manipulation. The code works ok, but the problem is I can't figure out how to get the upload error, if occured, back in the controller in order to pass it to the view and display it to the user, or the upload data if the uploading was done successfully.

This is an example of the code in my model:

PHP Code:
class Foo_Model extends CI_Model
{

    public function 
do_upload(){
        
$id intval($this->input->post('id'));
        
$config = array(
            
'upload_path' => './uploads/files/',
            
'allowed_types' =>  'gif|jpg|png',
            
'max_size'      =>  '2048',
            
'max_width'     =>  '800',
            
'max_heigth'    =>  '300',
            
'overwrite'     =>  true,
            
'file_name'     =>  'file_'.$id// e.g. file_10.jpg
        
);
        
$this->load->library('upload'$config);

        if ( ! 
$this->upload->do_upload('file') ) {

            return 
$this->upload->display_errors();
        } else {
            
// file uploaded successfully
            // now lets create some thumbs
            
$upload_file $this->upload->data();
            if (
$upload_file['is_image']) {
                
$config['image_library'] = 'gd2';
                
$config['source_image'] = $upload_file['file_name'];
                
$config['create_thumb'] = TRUE;
                
$config['maintain_ratio'] = TRUE;
                
$config['width'] = 75;
                
$config['height'] = 50;

                
$this->load->library('image_lib'$config);

                
$this->image_lib->resize();
            }

            
// uploading and resizing was done
            
return $upload_file;
            
// return true;
        
}
    }


The code inside the controller

PHP Code:
public function upload(){

    
$this->foo_model->do_upload();
    
// need to get the upload error (if any occured) or the upload data
    // how can I get them back from function of the model?

    
$this->load->view('form_upload'$data);


Thanks in advance


RE: Fat models - skin controllers with File Uploading - InsiteFX - 12-22-2014

do_upload should be in your controller not your model, models are for business logic like database access of records etc;


RE: Fat models - skin controllers with File Uploading - no1youknowz - 12-22-2014

(12-22-2014, 04:37 AM)InsiteFX Wrote: do_upload should be in your controller not your model, models are for business logic like database access of records etc;

I'm not going to comment on the above code, because I use ajax for my uploading.

But I have 6+ different "uploads" within my application.  Do all 6 controllers have the logic to handle the uploading?  Of course not, this would be silly to maintain 6 different code functions.

Instead, I have 1 main upload "library" which each controller interfaces with and returns the data when an upload is successful or not.

Ultimately, it's up to you what you decide to do.  I never saw the logic in having models hold anything except database logic.  Especially when you want to move away from CI.


RE: Fat models - skin controllers with File Uploading - Lykos22 - 12-23-2014

(12-22-2014, 04:37 AM)InsiteFX Wrote: models are for business logic like database access of records etc;

True! But doesn't uploading and image manipulation belong to business logic too? or at least their configuration settings (mean the two $config arrays)?


RE: Fat models - skin controllers with File Uploading - Avenirer - 12-23-2014

(12-23-2014, 01:15 AM)Lykos22 Wrote:
(12-22-2014, 04:37 AM)InsiteFX Wrote: models are for business logic like database access of records etc;

True! But doesn't uploading and image manipulation belong to business logic too? or at least their configuration settings (mean the two $config arrays)?
not according to the definition accepted by CodeIgniter Smile