Setting new users custom attributes
Permalink
Hi I have recently followed some documentation to create new users from a csv file programatically. According to the Concrete5 docs/api there was method called getByID( $uID ) but this has since be deprecated!
I am creating a new user like so:
$userRegistration = Core::make('user/registration');
$file = fopen("test-users.csv","r");
while (($data = fgetcsv($file)) !== FALSE) {
echo "email address " . $data[0];
$userRegistration->create([
'uName' => $data[0],
'uPassword' => $data[1],
'uEmail' => $data[2],
'uIsValidated' => true,
]);
}
However if I want to add a value to an existing to a non-core attribute for instance lets call it user_county then how would I change this after programatically the user? I may need to do this for several user attributes as well so the values would come from the CSV and automatically be looped through to apply the correct value to the corresponding attribute whether it is blank or filled.
I am creating a new user like so:
$userRegistration = Core::make('user/registration');
$file = fopen("test-users.csv","r");
while (($data = fgetcsv($file)) !== FALSE) {
echo "email address " . $data[0];
$userRegistration->create([
'uName' => $data[0],
'uPassword' => $data[1],
'uEmail' => $data[2],
'uIsValidated' => true,
]);
}
However if I want to add a value to an existing to a non-core attribute for instance lets call it user_county then how would I change this after programatically the user? I may need to do this for several user attributes as well so the values would come from the CSV and automatically be looped through to apply the correct value to the corresponding attribute whether it is blank or filled.
Hi man up,
Thanks for your reply.
I think I have seen the syntax on that site, I am trying to write my code so the php can be executed through the command line so it won't be a "logged in" user. So using my code above, the user is created I then need to attach attributes to that user just after its created. I was thinking I could get the user by their username then set attributes. But I'm not sure. Potentially I would have a number of custom attributes that might change over time but for example.
Username, password, email, isUserValidated, region, accountType,businessRole
User, password, user1@email.com,true, South, normal user, deputy ceo
Thanks for your reply.
I think I have seen the syntax on that site, I am trying to write my code so the php can be executed through the command line so it won't be a "logged in" user. So using my code above, the user is created I then need to attach attributes to that user just after its created. I was thinking I could get the user by their username then set attributes. But I'm not sure. Potentially I would have a number of custom attributes that might change over time but for example.
Username, password, email, isUserValidated, region, accountType,businessRole
User, password, user1@email.com,true, South, normal user, deputy ceo
You can do something like this
use UserInfo; $ui = Core::make('Concrete\Core\User\UserInfoFactory')->getByID($uID); $ui->setAttribute('attribute_handle', 'attribute value');
What if I don't know the ID that was just created? Can I use their username (as I would have defined that in my csv) how would I go about that?
You could do this
Here is the UserInfo documentation -http://documentation.concrete5.org/api/source-class-Concrete.Core.U...
use Core; Core::make('Concrete\Core\User\UserInfoFactory')->getByName($uName);
Here is the UserInfo documentation -http://documentation.concrete5.org/api/source-class-Concrete.Core.U...
I am getting "Class Concrete\Core\User\UserInfoFactory does not exist"
if I keep the "use Core" it says unexpected word "use"
any ideas whats going on there
if I keep the "use Core" it says unexpected word "use"
any ideas whats going on there
What version are you using?
Version 8.1.0 currently. Looked at 8.2.0 release candidate in the hopes it had Import users from CSV but it doesn't just the export to CSV
I'm not sure why that doesn't work, but this should work instead, just one more step.
use User; $u = User::getByUserID($uID); $ui = $u->getUserInfoObject();
Wouldn't I need to know the ID that has been created for that to work? or can you put the username in instead of ID?
Unfortunately yes, the only way I know to get the UserInfo by username is to use this code
Which as you have pointed out is deprecated, but it still works.
If you don't want to use that then you'll need the uID and use this
The code in the UserInfo class uses this, but you said it doesn't work (and it doesn't work for me either)
use UserInfo; $ui = UserInfo::getByUserName($username);
Which as you have pointed out is deprecated, but it still works.
If you don't want to use that then you'll need the uID and use this
use User; $u = User::getByUserID($uID); $ui = $u->getUserInfoObject();
The code in the UserInfo class uses this, but you said it doesn't work (and it doesn't work for me either)
/** * @deprecated */ public static function getByUserName($uName) { return Core::make('Concrete\Core\User\UserInfoRepository')->getByName($uName); }
And actually I guess I must have had mistake before, when I try this it does work for me
$ui = Core::make('Concrete\Core\User\UserInfoRepository')->getByName($username);
I just gave that a test in a PHP Code block and it seems to work on the front end! I think you may have solved my issue but I will have to test it via the user import script I am writing!
Thanks very much!
Side note, I did do another post about this but are you aware of how to use
$user_attributes = getAttribute("handle_name")
and get the attribute as an array? if I echo out $user_attributes, it justs prints it as a string like:
Attribute 1 Attribute 2
instead of something like:
$user_attributes[0] = "Attribute 1";
$user_attributes[1] = "Attribute 2";
Thanks very much!
Side note, I did do another post about this but are you aware of how to use
$user_attributes = getAttribute("handle_name")
and get the attribute as an array? if I echo out $user_attributes, it justs prints it as a string like:
Attribute 1 Attribute 2
instead of something like:
$user_attributes[0] = "Attribute 1";
$user_attributes[1] = "Attribute 2";
If I were to set an attribute with more than one option i.e multiple selected options. How would I go about doing so? Do I need to do :
$ui->setAttribute('handle', ["opt1","op2","op3"]);
or is there another way?
$ui->setAttribute('handle', ["opt1","op2","op3"]);
or is there another way?
Hi dkw15,
It looks like you also posted this question to Stack Overflow.
Did you review the posted answer?
https://stackoverflow.com/a/44781425...
It looks like you also posted this question to Stack Overflow.
Did you review the posted answer?
https://stackoverflow.com/a/44781425...
Yes I did review it MrKDilkington, though I wasn't sure how I could implement it in my own code.
In C5 Address is treated as a single attribute which contains Address, City, State and Country
I can help you to provide proper code.
is there a way you can provide the format of your csv file (test values, single row) ?