Welcome Guest, Not a member yet? Register   Sign In
Model method not inserting data into database
#1

Hey everyone,

I am really having a hard time figuring out something that looks so strange.

Here is what I want to do: each time a user updates profile using the "update" method in ion_auth, I am logging user activity in the database in a table called "activity". I created a model with a method "log" that has two arguments: UserId and the ActivityMessage.

When I call this "log" method in the user controller 'changepassword' method, it works fine. It inserts the activity to the table. BUT when I call this method inside an if statement that updates user profile, the "log" method gets called successfully BUT never inserts data to the "activity" table. However, the auto-increment ID of the activity table gets incremented if I manually insert the row into the database.

The following works (log method inserts data successfully):
PHP Code:
if ($this->ion_auth_model->changepassword($identity$current_password$password))
{
    
//log user activity
    
$this->activity_model->log_activity($this->ion_auth->user()->row()->id'changed password.');
    
redirect('user/profile','refresh');


BUT the following will NOT insert data to database (however the log method gets called):
PHP Code:
if($this->ion_auth_model->update($id$data)) {
    
$this->activity_model->log_activity($this->ion_auth->user()->row()->id'changed profile data.');
    
redirect('user/profile','refresh');


I even created a helper function to log user activity in the database, so I called the helper function inside the if($this->ion_auth_model->update($id, $data)) {...} block, but STILL it did not populate the table. But if I call that function in the same controller in the ion_auth change password method, it successfully logs data.

Sorry for the long description. But I am really pulling my hair on this!! I tried tons of ways to find out why it does not work!

Please help!!
Reply
#2

(This post was last modified: 08-26-2015, 05:46 AM by Avenirer.)

did you try this?


if($this->ion_auth->update($id, $data)) {
    $id = $this->ion_auth->user()->row()->id;
    $this->activity_model->log_activity($id, ' changed profile data.');
    redirect('user/profile','refresh');
}
Reply
#3

Thanks Avenirer. I tried this, and I even passed a value instead of a variable for the $id, still nothing happens.
Reply
#4

Can you show us the log_activity() method in your model?
Reply
#5

(This post was last modified: 08-26-2015, 10:12 PM by seasenx6.)

(08-26-2015, 05:43 AM)Avenirer Wrote: did you try this?


if($this->ion_auth->update($id, $data)) {
    $id = $this->ion_auth->user()->row()->id;
    $this->activity_model->log_activity($id, ' changed profile data.');
    redirect('user/profile','refresh');
}


(08-26-2015, 09:37 AM)cirox Wrote: Thanks Avenirer. I tried this, and I even passed a value instead of a variable for the $id, still nothing happens.

1. - $id = $this->ion_auth->user()->row()->id; <--- should return row_array();

example
PHP Code:
public function get_id ($username,... up to u) {
 
 $this->db->select('id');
 
 $this->db->from('table');
 
 $this->db->where('username'$username);
 
 $query $this->db->get();
 
 return $query->row_array(); //!important


2. - $this->activity_model->log_activity($id['id'], ' changed profile data.'); <--- using $id['id']; id is fied in database

still not happen again, ple check function in activity_model.
Reply
#6

Using row_array() instead of row() simply changes how you must reference the data afterwards, and wouldn't help unless there was a typo in the existing code or there was a reason to pass an array instead of an object. In this case, $id should be a value retrieved from the row, hence $this->ion_auth->user()->row()->id (using row_array() would make it $row = $this->ion_auth->user()->row_array(); $id = $row['id']Wink.

More than likely, the issue is either that the id is an unexpected value or there is a problem inside the log_activity() method.
Reply
#7

(08-26-2015, 10:40 AM)Wouter60 Wrote: Can you show us the log_activity() method in your model?

Hi,

Here is the log_activity function:

PHP Code:
    public function log_activity($user_id=''$message='') {
        
$data = array(
                
'user_id' => $user_id,
                
'message' => $message
            
);
        
$this->db->insert('activity'$data);
    } 
Reply
#8

(08-27-2015, 11:48 AM)mwhitney Wrote: Using row_array() instead of row() simply changes how you must reference the data afterwards, and wouldn't help unless there was a typo in the existing code or there was a reason to pass an array instead of an object. In this case, $id should be a value retrieved from the row, hence $this->ion_auth->user()->row()->id (using row_array() would make it $row = $this->ion_auth->user()->row_array(); $id = $row['id']Wink.

More than likely, the issue is either that the id is an unexpected value or there is a problem inside the log_activity() method.

I agree with you. I am not returning any data from the function. On the other hand, I tried to pass values instead of variables in the parameters of the function, still it won't work. However, the same function, while used outside of the if(....) statement, would work fine. But even if I echo some random stuff to test if the "if" statement returns true, I can echo it out...it works on that end. 
Reply
#9

It looks like there must be something going on we aren't seeing, because the code should work as written. Some things that come to mind as potential causes would be use of Query Builder Caching ($this->db->start_cache()), a database error, or an event handler (since Ion Auth has an event system).
Reply




Theme © iAndrew 2016 - Forum software by © MyBB