Welcome Guest, Not a member yet? Register   Sign In
Model method not inserting data but gets called successfully
#1
Sad 

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. It looks like it inserts the row and deletes it automatically?

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($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($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)) {..helper method here..} 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

If you set up a test on $this->ion_auth_model->update($id, $data) does it work?

I would suspect that if your IF statement is being parsed as TRUE then something other than FALSE is being returned, perhaps an error message? Is your $data constructed with the right information?

My first approach would be to validate the $data field, if that was as you intended it to be, is it what ion_auth expected (check the docs) or check the call works by itself. The fact that $this->ion_auth_model->changepassword is working does not say anything about your $this->ion_auth_model->update method.

Hope you get it fixed,

Best wishes,

Paul.
Reply
#3

(08-25-2015, 04:34 PM)cirox Wrote: 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. It looks like it inserts the row and deletes it automatically?

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($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($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)) {..helper method here..} 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!!

It will not insert any data to your database because you called the UPDATE method. Use the insert function in your model.



Code:
$data = array('field1' => 'value1', 'field2' => 'value2', ...);
$this->db->insert('tablename', $data);

Hope it may help!
Reply
#4

Enable the profiler and inspect which SQL queries are being fired .
          Heart  love codeigniter Heart
          Learning  best  practices
     Rate my post if you found it helpfull
Reply




Theme © iAndrew 2016 - Forum software by © MyBB