Getting user attribute values in 5.3.3
$ui = UserInfo::getByID(20); $fname = $ui->getUserFname(); $lname = $ui->getUserLname();
Does anyone know how to do this in 5.3.3? I suspect it is something simple, but I'm not very good at digging through the code to figure these things out.
// so if the attrib handle is "my_attribute", then get the attribute with $ui->getUserMyAttribute(), or "uFirstName" become $ui->getUserUfirstname();
So I would *guess*:
<?php Loader::model('user'); $ok = UserInfo::getByUserName('user'); echo $ok->getAttribute('date_of_birth');
Not sure if there's another way.
I have been importing users and their attributes easily via a PHP script, but C5 has changed the database structure a lot with the 5.3.3 release. I haven't sorted out the differences yet, but it looks a lot different and more complicated, ie the UserAttributeValues and UserAttributeKeys tables now store ids instead of handles and values. Looks like the values and handles are stored in the tables name with underscores. Can I update the _UserAttributeValues and _UserAttributeKeys tables with my script or will there be side effects? Can anyone give me insight of the differences and how I can use my own scripts? Does anyone else think Concrete5 needs a better API or am I alone on this? Thanks!
The only drawback to this new system is that it is indeed more complicated to read the information directly back from the database.
I'll address a couple of your points.
1. API for getting/setting user attribute values. It should be easy to do this (and this actually hasn't changed.)
<?php $ui = UserInfo::getByID($theUserID); $ui->setAttribute('attribute_handle', $attributeValue); ?>
To get the user's "gender" for example,
<?php $ui = UserInfo::getByID($theUserID); print $ui->getAttribute('gender'); ?>
There are even magic methods for getting attribute values (just for user info objects, however)
<?php $ui = UserInfo::getByID($theUserID); print $ui->getUserGender(); ?>
Per the example above, if you had an attribute with the handle "my_awesome_attribute" you could run UserInfo::getUserMyAwesomeAttribute() and it would return that value.
The new attribute system really really is much improved. You can build your own attribute types at this point, which means storing complex objects as attributes (e.g. the "address" attribute we have in there, the much improved "select" attribute type. )
Does this help?
Thanks this was great.
The new database structure is mind boggling with all it's id's to tables with ids and I couldn't even figure out where data for user attributes is being stored now, it's not in UserAttributesValues or AttributeValues, just more ids. I am using the Users table for my members and I need to run queries for my own server side processes. I am starting to regret using the Users table for my site members. Can someone please advise? Thanks!
How can we do searches on custom User Attributes? I did figure out how to include custom attributes in the user_list model, you can go to Users & Groups -> User Attributes and then tick the box under Searchable for Content included in "Keyword Search". But sometimes I want to search on a select few columns, not all of them. Also, how can we do searches on values per custom attributes that returns list of users? And what about querying for values compared for NOT equal.
Answer: i.e. for user_list model use $userList->filterByAttribute(attrHandle, value, comparison)
How do we add a user and it's custom attributes via the API? I didn't find any API methods for adding a new user and it's attributes via the User object nor could I find any examples in the forum posts.