Populate field with logged in user details from users table

A place for users and developers of the Xataface to discuss and receive support.

Populate field with logged in user details from users table

Postby tomhousley » Thu Feb 26, 2009 1:13 am

Hello all,

I have a number of tables, two are called:
tbl_users (a list of users used to login)
tbl_orghistory (a table to record contact information on customers)

In the table tbl_orghistory, there is a field called his_enteredby and this is automatically populated by the username of the logged in user using the delegate class called tbl_orghistory.php which looks like:

class tables_tbl_orghistory {
function his_enteredby__default(){
$auth =& Dataface_AuthenticationTool::getInstance();
return $auth->getLoggedInUsername();

}

}

This is okay, but i want to automatically populate the field his_enteredby with the ID of the user from field usr_id

The Users table is shown below:

usr_id int(6)
usr_username varchar(32)
usr_password varchar(32)
usr_role enum('READ ONLY','NO ACCESS','ADMIN')
usr_firstname char(255)
usr_lastname char(255)
usr_status int(6)

Any help would be greatly appreciated.

Many thanks, Tom
tomhousley
 
Posts: 52
Joined: Thu Feb 26, 2009 1:02 am
Location: United Kingdom

Postby shannah » Thu Feb 26, 2009 8:46 am

Hi,

There are 2 ways to do this. One is using default values as you have done. Another is to use a beforeInsert() trigger. THis is the way that I usually do it.

e.g.
Code: Select all
function beforeInsert(&$record){
    $auth =& Dataface_AuthenticationTool::getInstance();
    $user =& $auth->getLoggedInUser();
    if ( $user ) $record->setValue('his_enteredby', $user->val('usr_id'));
}


And then set the widget type for this field to hidden so that the user doesn't attempt to change it.

Of course the same thing could be done using the default value method as you are already using.

The key that you were looking for I believe is that you should use the getLoggedInUser() method instead of getLoggedInUsername() to obtain the Dataface_Record object for the currently logged in user. That way you can access any information about the user.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby tomhousley » Thu Feb 26, 2009 9:43 am

Thank you Steve,

For some reason I couldn't get your code to work (it didnt place value into field but gave no errors), so i came up with this, which does work:

function his_enteredby__default(){
$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();
return $user->val('usr_id');


If you are able to tell me why mine worked and yours didn't in this case, I would be grateful.

Many thanks,

Tom
tomhousley
 
Posts: 52
Joined: Thu Feb 26, 2009 1:02 am
Location: United Kingdom

Recording logged-in user information

Postby fredly » Tue Apr 14, 2009 5:37 am

I've tried the 2 methods mentioned above (earlier) and, like Tom, I can't get Steve's beforeInsert() trigger method to work. Tom's default() method does work for NEW records, but does not seem to work when changes to EXISTING records are made. There's also some earlier discussion at:

http://xataface.com/forum/viewtopic.php?t=4215#21205
http://xataface.com/forum/viewtopic.php?t=4587#22648

What I'm aiming to achieve is to 'auto-fill' a field called 'updated_by' in my main table with the currently logged-in user's UserID or UserName (in my User table).

Any reason why the 2 above methods don't appear to work for this task? (Maybe I'm going something wrong.)

Fred
fredly
 
Posts: 7
Joined: Fri Mar 27, 2009 2:07 pm

Postby shannah » Tue Apr 14, 2009 10:25 pm

There is no reason why the beforeInsert() record shouldn't work. I use this method regularly. If it isn't working for you, take a closer look, do some experimentation and find out where the bug is.

Things to check:
1. Make sure beforeInsert() is defined in the correct table.
2. Make sure thate the beforeInsert() method is at least executed. (E.g. add a die statement to output some text).
3. Check the value that is coming out of your $user->val('usr_id') call.
4. Make sure that $user isn't null (i.e. a user is logged in).

Usually these problems amount to a tiny programming error... you just have to get your hands a little dirty to uncover it.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 11 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved