Getting user attribute values in 5.3.3
Permalink
It seems that the newest version of C5 has changed the way it retrieves user attribute values. In 5.3.2 the following code retrieved fname and lname values for the user with the userID 20:
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.
$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.
Apparently the info was not saved in the database and this is why the methods were not working. I'm not sure if it was lost when I upgraded from 5.3.2 to 5.3.3 but it was there at one point and then it was gone.
Use the getAttribute method of UserInfo model.
Not sure if there's another way.
<?php Loader::model('user'); $ok = UserInfo::getByUserName('user'); echo $ok->getAttribute('date_of_birth');
Not sure if there's another way.
I need to store and retrieve User and Attribute values. I gave the API methods a try initially but abandoned it because it seems unintuitive.
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!
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!
Hah - one of the main goals of 5.3.3 was a unification of all the different ways we're dealing with attributes in our system. So that means one central system of storing attributes, and unique tables that map those attribute values to objects like user, page, etc...
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.)
To get the user's "gender" for example,
There are even magic methods for getting attribute values (just for user info objects, however)
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?
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?
You should put a "Save to file" button on these. Getting tired of cut-n-paste.
Thanks this was great.
Thanks this was great.
I need the ability to run scripts outside of the CMS directory. Is there a bootstrap include file so I have can access to these functions for external processing?
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!
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!
but have you tried doing it with single pages?
I would prefer not to be forced to run pages within the cms directory. Ideally I would like to include a file that uses an API authentication key to enable calling API functions for custom processes for Users, etc.
I couldn't figure out where the data was stored, it wasn't in UserAttributes or Attribute Values. Is there documentation on how the structure has changed and which tables and columns the user data is now stored?
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 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)
Ugh, so I'm trying to convert my select queries using the API and it's been time consuming and the queries are inefficient and slower to process. The single page code totally hung when processing a third-party process, but works fine when using custom queries accessing UserSearchIndexAttributes in in a script outside of Concrete's cms. I can no longer do complex selects nor query my own custom table data against fields relative to concrete5 tables without breaking it up to multiple queries. As far as I can tell there is no way to do IN() select statements or left or right joins with custom tables using the current API. Also, I don't see a way to grab the query string to echo it out during debugging.
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.
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.
// so if the attrib handle is "my_attribute", then get the attribute with $ui->getUserMyAttribute(), or "uFirstName" become $ui->getUserUfirstname();
So I would *guess*:
$ui->getUserUfirstname()
and
$ui->getUserUlastname()