CodeIgniter Forums
Model problems - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Model problems (/showthread.php?tid=9307)

Pages: 1 2


Model problems - El Forum - 06-20-2008

[eluser]stuffradio[/eluser]
How would you do a model for a user table.

This is mine but it isn't updating.

Code:
<?php

class Users extends Model
{
var $id;
var $firstName;
var $lastName;
var $username;
var $password;
var $email;
var $gender;
var $location;
var $website;
var $about;
var $allow_comments;
var $email_subscriber;
var $comment_email;
var $subscriber_email;
var $email_pm;
var $safe_browsing;
var $tile;

function Users()
{
  parent::Model();
}

function update()
{

$update = array(
              'location' => $this->location,
              'about' => $this->about,
              'website' => $this->website,
              'allow_comments' => $this->allow_comments,
              'email_subscriber' => $this->email_subscriber,
              'comment_email' => $this->comment_email,
              'subscriber_email' => $this->subscriber_email,
              'email_pm' => $this->email_pm,
              'safe_browsing' => $this->safe_browsing
              );

  
  if ($this->id)
  {
   $this->db->where('id', $this->id);
   $this->db->update('project_users', $update);
  }

}

}



Model problems - El Forum - 06-21-2008

[eluser]treehousetim[/eluser]
Assuming you're setting the values into the proper variables in your model, and assuming you're calling the update method, this should work.

Care to share your controller?


Model problems - El Forum - 06-21-2008

[eluser]stuffradio[/eluser]
Code:
function account_save()
   {
              $location = $this->input->post('location', True);
              $about = $this->input->post('about', True);
              $website = $this->input->post('website', True);
              $allow_comments = $this->input->post('allow_comments');
              $email_subscriber = $this->input->post('email_subscriber');
              $comment_email = $this->input->post('comment_email');
              $subscriber_email = $this->input->post('subscriber_email');
              $email_pm = $this->input->post('email_pm');
              $safe_browsing = $this->input->post('safe_browsing');
              $id = $data['account']->id;
              
    $this->users->location = $this->location;
    $this->users->about = $this->about;
    $this->users->website = $this->website;
    $this->users->allow_comments = $this->allow_comments;
    $this->users->email_subscriber = $this->email_subscriber;
    $this->users->comment_email = $this->comment_email;
    $this->users->subscriber_email = $this->subscriber_email;
    $this->users->email_pm = $this->email_pm;
    $this->users->safe_browsing = $this->safe_browsing;
    $data['update'] = $this->users->update();

    redirect('settings/account');
   }



Model problems - El Forum - 06-21-2008

[eluser]treehousetim[/eluser]
You are not setting $this->users->id from your $id variable, but your model is trying to use it to update.

And since you're doing a redirect, you're probably not seeing an error. Try commenting out the redirect to see what you can see.


Model problems - El Forum - 06-21-2008

[eluser]treehousetim[/eluser]
Actually there wouldn't be an error...

It's just never going to execute the db->update since the if statement will evaluate to false
Code:
if ($this->id)
  {
   $this->db->where('id', $this->id);
   $this->db->update('project_users', $update);
  }



Model problems - El Forum - 06-21-2008

[eluser]stuffradio[/eluser]
I tried that, but it still won't work for some reason, hmmm.


Model problems - El Forum - 06-21-2008

[eluser]treehousetim[/eluser]
Did you try this?
Code:
$this->users->id =$id;



Model problems - El Forum - 06-21-2008

[eluser]stuffradio[/eluser]
Yup.


Model problems - El Forum - 06-21-2008

[eluser]treehousetim[/eluser]
I don't see where $data is being set - why don't you try commenting out the redirect and echoing the value of $id - it may not be evaluating to a true condition.


Model problems - El Forum - 06-22-2008

[eluser]stuffradio[/eluser]
No, it's evaluating correctly.