2

I've been working with the Tank Auth library all day and have a question about it. I added two fields to the registration form: first_name and last_name respectively and I'm trying to find out why its not inserting into the user_profiles page.

With the updated code I'm getting this error:

A PHP Error was encountered

Severity: Warning

Message: Missing argument 5 for Tank_auth::create_user(), called in /home/xtremer/public_html/kowmanager/application/controllers/auth.php on line 136 and defined

Filename: libraries/Tank_auth.php

Line Number: 162 A PHP Error was encountered

Severity: Notice

Message: Undefined property: Tank_auth::$users

Filename: libraries/Tank_auth.php

Line Number: 188

Fatal error: Call to a member function UpdateProfileInfo() on a non-object in /home/xtremer/public_html/kowmanager/application/libraries/Tank_auth.php on line 188

Updated Code:

Auth Controller

Tank Auth Library

Users Model

Any ideas on what I am doing wrong now?

I'll put a 50 point bounty on this but I can't get the link to come up so that there is a bounty.

Community
  • 1
  • 1
Jeff Davidson
  • 1,921
  • 7
  • 36
  • 60

3 Answers3

8

What You should do is put the two columns you want in the user_profiles table, then add a function to the models/tank_auth/users.php with something like:

function UpdateProfileInfo ($userID, $firstname, $lastname)
{
    return $this->db->update('user_profiles', array('firstname'=>$firstname, 'lastname'=>$lastname), array('user_id' => $userID)); 
}

Then replace (in /libraries/Tank_auth.php)
function create_user($username, $email, $password, $email_activation)
With
function create_user($username, $email, $password, $email_activation, $userInfo)

Then right beneath (in /libraries/Tank_auth.php)
if (!is_null($res = $this->ci->users->create_user($data, !$email_activation))) { Add
$this->users->UpdateProfileInfo($userInfo["firstname"],$userInfo["lastname"]);

Then replace (in /controllers/auth.php)

        if ($this->form_validation->run()) {                                // validation ok
            if (!is_null($data = $this->tank_auth->create_user(
                    $use_username ? $this->form_validation->set_value('username') : '',
                    $this->form_validation->set_value('email'),
                    $this->form_validation->set_value('password'),
                    $email_activation))) {                                  // success

with:

$userInfo["firstname"] = $this->form_validation->set_value("firstname");
$userInfo["lastname"]  = $this->form_validation->set_value("lastname");

if ($this->form_validation->run()) {                                // validation ok
    if (!is_null($data = $this->tank_auth->create_user(
            $use_username ? $this->form_validation->set_value('username') : '',
            $this->form_validation->set_value('email'),
            $this->form_validation->set_value('password'),
            $email_activation, $userInfo))) {                                   // success

This isn't tested though it should work, tell me how it goes
Max

Jess
  • 8,628
  • 6
  • 49
  • 67
  • There's already a function in the library that handles that. Do you have any experience with this library? – Jeff Davidson Aug 16 '11 at 01:12
  • I've updated my code to show what is already included with the library for handling it. Its not inserting into the user_profiles page. – Jeff Davidson Aug 16 '11 at 03:43
  • 2
    I do actually am familiar with the library, and I know there is a createprofile function. I recommended putting it in a different function so you could reuse it if the user changes their information (say you also included city/state information in this function). – Jess Aug 16 '11 at 03:45
  • But why isn't it inserting into the db right now out of curiosity. – Jeff Davidson Aug 16 '11 at 03:47
  • 1
    Because when a user registers, the profile row is automatically created (it's in the create_user function in the models/tankauth/users.php model) with null for each of the columns (except the user_id column), so all we need to do is update that row with the additional information we have. I did make one error though. You should put the update code (the bit that calls `AddProfileInfo`) into the tank_auth library after `if (!is_null($res = $this->ci->users->create_user($data, !$email_activation))) {` not in the auth contrller – Jess Aug 16 '11 at 04:50
  • So with the my code above what do I need to do to correct the issue so it will insert into the profiles table. – Jeff Davidson Aug 16 '11 at 04:53
  • 1
    I will tweak my above answer, but remember you are updating a row that has been created when the user was created. – Jess Aug 16 '11 at 05:16
  • @mazzzzz let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2510/discussion-between-jeff-davidson-and-mazzzzz) – Jeff Davidson Aug 16 '11 at 05:26
  • I'm talking about the code above in my post though. Meaning the code that already exists. I'm trying to figure out and understand why its not updating with the code I have. – Jeff Davidson Aug 16 '11 at 05:32
  • I updated my post and code with what I made for yours as well as a error that I"m currently getting. – Jeff Davidson Aug 16 '11 at 23:17
  • 1
    This solution worked for me with some minor adjustments: `code$this->users->UpdateProfileInfo($userInfo["firstname"],$userInfo["lastname"]);` should be `code$this->ci->users->UpdateProfileInfo($userInfo["firstname"],$userInfo["lastname"]);` And for some reason I couldn't pass `code`$userInfo`code` as an array in `codecreate_user($username, $email, $password, $email_activation, $userInfo)`. I had to pass $userInfo as a single value. – chromaloop Aug 17 '14 at 12:00
2

I noticed a couple things:

private function create_profile($user_id, $data)
{
    $this->db->set('user_id', $user_id);
    $this->db->set('first_name', $first_name);
    $this->db->set('last_name', $last_name);
    return $this->db->insert($this->profile_table_name);
}

$data is an array, I'm assuming you SHOULD pass first_name and last_name here (which you do not).

Also TANK AUTH requires you to updated the columns you need for profile database schema (did you do this? didn't mention).

To correct the code above, you would need to pass more details in the array ($data) like so:

$data['first_name'] = "Bob";
$data['last_name']  = "Smith";

create_profile($user_id, $data); // which would then use the first & last names 

Not that I want to keep a conversation going about this but...*

You need to do just what I showed you:

  • define 2 variables (first & last name) and pass them to the create_profile fn.
  • properly USE the variables (don't use $data[0] that is SLOPPY, do $data['first_name'] if that is what you called the array value. There is no reason to be sloppy and do $data[0] -- guessing no less at the key value of your array).
  • its not hard, read your code (understand it, if you don't then step back and try to break it down line by line what is happening, I am getting the sense you have NO idea what any of these functions are doing).
Jakub
  • 20,418
  • 8
  • 65
  • 92
  • I did update the database Jakub. The question is where wold I put the $data['first_name'] part – Jeff Davidson Aug 16 '11 at 05:44
  • Well, quite simply where the fn `create_profile($userid, $data)` is called, so I see it in `create_user()` fn. – Jakub Aug 16 '11 at 05:47
  • So your saying it shoudl be private function create_profile($user_id, $data) { $this->db->set('user_id', $user_id); $this->db->set('first_name', $data[0]); $this->db->set('last_name', $data[1]); return $this->db->insert($this->profile_table_name); } because I don't know. – Jeff Davidson Aug 16 '11 at 05:51
  • answer updated, please just re-read your code, this is straight forward passing var to a fn(), if you don't understand that you need to review / learn what TANK AUTH is doing. – Jakub Aug 16 '11 at 05:56
  • That's exactly what I have in my code though which means its not going to work. – Jeff Davidson Aug 16 '11 at 06:01
  • 1
    I still think you are confusing yourself Jeff, the code you posted DOES NOT reflect you doing it correctly. You just randomly added `$this->form_validation->set_value('first_name'), $this->form_validation->set_value('last_name'),` into `create_user` fn() – Jakub Aug 16 '11 at 06:09
  • @Jakub let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2515/discussion-between-jeff-davidson-and-jakub) – Jeff Davidson Aug 16 '11 at 06:12
1

Check the Tank_auth file in the library, there is another "create_user" function that you have to modify. To manage properly the variables. 3 hours breaking my head with this.

Jimmy
  • 11
  • 1