Welcome Guest, Not a member yet? Register   Sign In
Models - returns array
#1

[eluser]xoRiZ[/eluser]
I've come to use this very frequently, as I've noticed in all my models, I want my functions to return 3 things.

1) Did it error?
2) Message (erroneous or not)
3) Result set


In retrospect, perhaps this should be an object, instead of an array.. But oh well.

Essentially, you pass this function( (bool) Error, (string) Message[, (mixed) Result])

It will log if there's an error, and include the query. If you pass anything as the result in the event of an error, you can log it, without it being displayed to the user.

I'm thinking for multilingual support, you would pass the language key as the message and parse it in this function and to the user.


return_helper.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('return_result'))
{

    // Purpose of this function is to keep a standardized return result for all functions utilizing a result set, and allow for a message to be displayed to the user.
    function return_result($error, $message, $result = NULL)
    {
        if ( TRUE === function_exists('log_message') && TRUE === $error )
        {
            if (FALSE === empty($result))
            {
                $message .= "\n\n".$result;
            }
            
            log_message('error', $message);
            
        }
    
        return array(
            'error' => $error,
            'message' => $message,
            'result' => $result
            );
    }
}


/* End of file return_helper.php */
/* Location: ./framework/version/helpers/return_helper.php */




Example usage:

Model:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model
{

    function __construct()
    {
        parent::__construct();
        
    
    }
    
    public function create_user($data)
    {
        
        
        // Check for existing usernames
        
        if (TRUE === $this->username_exists($data['username']))
        {
            return FALSE;
        }
        // I scrub data here. I haven't released this helper yet.

        
        $insert = array(
            
            'password' => $this->auth->hash_password($data['username'], $data['password']),
            );

        $response = $this->db->insert('users', $data);
        
        if (!$response)
        {
            return return_result(TRUE, 'Failed to create user.', $this->db->last_query());
        
        }
        
        if ($this->db->insert_id() > 0)
        {
            return return_result(FALSE, 'Successfully created user.', $this->db->insert_id());
        }
        
        return return_result(TRUE, 'Unknown error occurred creating user. Please try again.');
        
        
        
    }


Controller

Code:
class Agent extends MY_Controller {

    public function index()
    {
    
        // overview
        
    }
    
    

    public function create()
    {
        if ($this->input->post('submit'))
        {
            // Form submitted

            if ($this->form_validation->run('agent_create') === TRUE)
            {


                
                $result = $this->users->create_user($data);
                

                if (FALSE === $result['error'])
                {
                
                
                    // When you utilize a result set as $result['result'], you can simply assign it to the array going to your view.

                }
                else
                {
                // pull error message and display to user
                echo $result['message'];
                }
            }
        }
        $this->load_view('agent/create', FALSE);
    }



Some of this code has had large snippets removed .. But you should be able to draw the general idea behind the use of the array.


I'm not sure if doing this is in bad taste or not, but I welcome alternatives, if it is.


Messages In This Thread
Models - returns array - by El Forum - 08-30-2011, 04:00 PM



Theme © iAndrew 2016 - Forum software by © MyBB