Jump to content

saving user fields with added properties


bmacnaughton
 Share

Recommended Posts

I create a new user, add some custom properties to it, and save the user to the DB. But it appears that the custom properties are not saved to the DB. Is this expected behavior? What should I do to save custom properties in the DB?

$u = new User();
$u->name = $form->get('username')->value;
$u->pass = $form->get('password')->value;
$u->email = $form->get('email')->value;
$u->addRole('guest');

// add custom fields
$u->validation = $validation;
$u->timestamp = time();

// i can see in the log that the $user object has validation in it
$log->save("wrote validation: $validation to user (\$u->$u->validation)");

// save the user to the DB
$u->save();

Link to comment
Share on other sites

I think you didn't add those fields to the user template? If so you can still add variables like above, but these are runtime only and won't be save, as there are no columns in the database for these fields. You can add fields to the user template like you'd do it with every other template. It's just hidden by default so you need to enable "show system templates" on top of the templates-list.

  • Like 2
Link to comment
Share on other sites

  • 4 years later...

Hello,

I have created a standard field type as userphone , then I added this field to the User template. I created a phone field type using Phone Field module. I added this field to the User template also.

image.thumb.png.f40f3d23d3f8849fdf15e13a06ef63b6.png

Now I used the below to create a test user

 $u = new User();
$u->of(false);
$u->name = "saarun";
$u->email = "saarunthomas@example.com";
$u->pass = "123456";
$u->Phone = 9446196667;
$u->userphone = 9446196667;
$u->headline = "hello there";
$u->paid=1;
$u->addRole("registered");
$u->save();
$u->of(true);

and I tried to out put as below

     $item = $users->get("saarun");
echo $item->email;
echo $item->Phone;
echo $item->paid;
echo $item->headline;
echo $item->userphone;

and I got the output a below.  Neither the userphone value nor the Phone value are output ... what am I doing here wrong  ? pls help

 

image.png.eee59c5c952dd521d71cb1dd8d23cd22.png

Link to comment
Share on other sites

Hey, @JeevanisM!

You use an Integer Field for the phone number. Maybe that field cannot store such a big number as 9446196667? Test it with a 123 to check.

I guess you should store a phone number in some other field any way. Text would be ok (with some frontend and server validation). There are dedicated fields for this as well like this one.

  • Thanks 1
Link to comment
Share on other sites

On 11/13/2019 at 8:59 PM, Ivan Gretsky said:

I guess you should store a phone number in some other field any way. Text would be ok (with some frontend and server validation).

I'd strongly suggest that as well. A telephone number is not a "number" in the mathematical sense. You'll not do arithmetic on them, leading zeros are not optional, even though it's comprised mostly of digits there might be characters as well, …. Telephone numbers are actually identifiers and those are best stored in text or more specialized columns.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...