Welcome Guest, Not a member yet? Register   Sign In
Can't update a controller variable in model
#1

[eluser]mikertjones[/eluser]
Hi

This is frustrating

Controller Clients declares var:

Code:
var $vendor_id;

Controller adds or updates client records using model m_client.
If a vendor_id exists it updates the vendor record, if not it creates a new one.

So, in the Controller Clients.php

Code:
$this->m_client->save_vendor();

and in the model m_client.php

Code:
function save_vendor()
  {
   $num_items = empty($_POST['num_items'])?0:$this->input->post('num_items');
   $vendor_data = array(
    'client_id'=>$this->client_id,
    'auction_id'=>$this->auction_id,
    'vendor_ref'=>$this->input->post('auction_vendor_ref'),
    'reference_address'=>$this->input->post('reference_address'),
    'num_items'=>$num_items);

   if($this->vendor_id)
   {
     $this->db->where('vendor_id',$this->vendor_id);
     $this->db->update('vendor',$vendor_data);
   }else{
     $this->db->insert('vendor',$vendor_data);
     $this->vendor_id = $this->db->insert_id();
   }
} // END fn save_vendor

Testing in the context of the model, if a new vendor record is added, $this->vendor_id has a value.
Testing back in Client controller $this->vendor_id has no value even after the call to m_client->save_vendor.

I am expecting that if a new vendor record is inserted that the line

Code:
$this->vendor_id = $this->db->insert_id();

would update the $this->vendor_id value accessible back in the Controller - I need it to call another model function.

Any thoughts?

Thanks

Mike
#2

[eluser]boltsabre[/eluser]
You're close, but you must return your new vendor id from the model like this.

Code:
// controller
$vend_id = $this->m_client->save_vendor();
if($vend_id !== true){
   // not returning true, so must be a new vendor id from model, update our controller vendor_id
   $this->vendor_id = $vend_id;
}

Code:
//model
...
if($this->vendor_id)
   {
     $this->db->where('vendor_id',$this->vendor_id);
     $this->db->update('vendor',$vendor_data);
     return true
   }else{
     $this->db->insert('vendor',$vendor_data);
     return $this->db->insert_id();
   }

You cant just access variables set in a model direct from your controller, you must pass them back (otherwise you'd run the risk of "over riding" your controller variables from your model if you mistakenly called them both the same thing).
#3

[eluser]weboap[/eluser]
why not do?

in controller
Code:
$this->m_client->save_vendor( $vendor_id );

in the model

Code:
function save_vendor($vendor_id)
  {
   $num_items = empty($_POST['num_items'])?0:$this->input->post('num_items');
   $vendor_data = array(
    'client_id'=>$this->client_id,
    'auction_id'=>$this->auction_id,
    'vendor_ref'=>$this->input->post('auction_vendor_ref'),
    'reference_address'=>$this->input->post('reference_address'),
    'num_items'=>$num_items);

   if($vendor_id)
   {
     $this->db->where('vendor_id',$vendor_id);
     $this->db->update('vendor',$vendor_data);
   }else{
     $this->db->insert('vendor',$vendor_data);
     $this->vendor_id = $this->db->insert_id();
   }
} // END fn save_vendor
#4

[eluser]mikertjones[/eluser]
Thanks for feedback

boltsabre:

I managed a workaround which is a version of your reply.
After the call to save_vendor back in the Controller I allocated the vendor_id as:
Code:
$this->vendor_id = $this->vendor_id?$this->vendor_id:$this->m_client->vendor_id;

weboap:

Thanks but I can easily use the controller $this->vendor_id in the model if it exists - what I needed was to populate an empty $this->vendor_id in the model so that I can use it for other purposes back in the Controller.

Great to have some feedback.




Theme © iAndrew 2016 - Forum software by © MyBB