user1696090
user1696090

Reputation: 371

CodeIgniter: My upsert function not working with my_model / my_controller

Working with the my_model & my_controller for the first time this month within CodeIgniter, I think I almost have this working.

I got an insert function working properly, now I'm trying to add an update to it if there is an ID.

Here's my code:

function upsert_client($client_id = 0)
{
    load_model('client_model');
    $this->insertMethodJS();

    $this->fields['client'] = $this->_prototype_client();

    $user_id = get_user_id();
    $company_id = get_company_id();

    if ($client_id)
    {
        $this->data['client'] = $this->client_model->get_record($client_id);
    }   



    if (!$this->ion_auth->in_group(GROUP_NAME_MANAGER, $user_id))
    {
        redirect('members/dashboard');
    }

    if ($_POST)
    {
        $this->load->helper('string');  

        if ($this->_validate_client())
        {   

        $fields = $this->input->post(null , TRUE);
        $fields['user_id'] = $user_id;
        $fields['company_id'] = $company_id;
        $fields['active'] = 1;

            if ($client_id)
            {
            $fields['id'] = $this->client_model->get_record($client_id);    
            unset($fields['billing']);
            $this->client_model->update($client_id, $fields);       
            }

            else 
            {
            unset($fields['billing']);
            $this->client_model->insert($fields);
            redirect('members/clients/manage_clients');
            }

        }
    }

    $this->template->write_view('content',$this->base_path.'/'.build_view_path(__METHOD__), $this->data);           
    $this->template->render();
}



function _prototype_client()
{
    $fields = array();

    $fields['id']   = 0;
    $fields['name'] = '';           

    return $fields;
}

And from my client_model:

class Client_model extends MY_Model {

function get_record($client_id)
{

    $query = $this->db->select('id')
    ->where(array('id'=>$client_id))
    ->get('clients');
    return $query->row_array();
}

}

Everytime I try to edit a "client", it just inserts a new one... All I'm currently trying to edit is the "name" field.

My edit button:

<td><a href="add_client/<?= $client->id; ?>"><button class="btn btn-inverse" style="float: right;" type="button">Edit</button></a></td>

Any help is appreciated, thanks! And let me know if you need any additional details...

Upvotes: 1

Views: 1498

Answers (2)

dispake
dispake

Reputation: 3329

Your hyperlink points to 'add_client' whereas the function you are showing is called 'upsert' Are you calling the correct URL?

Upvotes: 1

user1296953
user1296953

Reputation: 135

I never worked with ion auth in particular but from what I see you have a couple of functions that are not referenced correctly.

load_model('client_model');
//Should be
$this->load->model('client_model');

also a few other functions should be referenced as

$this->function_name();
//Instead of just
function_name();
//Unless they are in another library
$this->lib_name->function_name();

I'm not sure if this well solve your problems but just a few things I noticed.

Upvotes: 1

Related Questions