codeigniter-base-model icon indicating copy to clipboard operation
codeigniter-base-model copied to clipboard

A question with Callbacks/Observers

Open appally opened this issue 12 years ago • 8 comments

after created a feedback, Send a message, But that does not work, I write code like this

class Feedback_model extends MY_Model { public $after_create = array('add_message');

protected function add_message($data)
    {       
    $msg = array('subject' => 'You have message from '.$data['name']);

    $this->db->insert('messages', $msg);        

            return $data;
     }

}

appally avatar Nov 24 '12 15:11 appally

Sorry, what does "does not work" mean? Are you getting any errors?

jamierumbelow avatar Nov 24 '12 15:11 jamierumbelow

Thank you for your reply, I can't get value of $data['name']

appally avatar Nov 24 '12 15:11 appally

DO you have a better way to get this work?

appally avatar Nov 24 '12 15:11 appally

I believe the after_create trigger only returns the insert id.

andytime avatar Nov 24 '12 16:11 andytime

So, if it's returning the newly inserted ID, you could do this:

$feedback = $this->get($data);
$feedback->name;

jamierumbelow avatar Nov 24 '12 16:11 jamierumbelow

Thank you very much ,that's works now

appally avatar Nov 24 '12 16:11 appally

sorry, i just want to post is :

class Feedback_model extends MY_Model { public $after_create = array('add_message');

protected function add_message($data)
{
 $msg = array(......);

 $this->message_model->add_msg($msg);

}

}

appally avatar Nov 24 '12 16:11 appally

Yeah, of course. It's a normal model, just load it like usual!

jamierumbelow avatar Nov 24 '12 16:11 jamierumbelow