How do I create a CActiveRecord with attributes not present in the table?

2.8k Views Asked by At

I have an Active Record Model for Users in my application, and I was hoping to re-use this model for the user registration form. The user registration form has a few fields which I don't want to store in the database, such as the password and password confirmation (I'm storing a salt and a hash instead). Is there a way to do this with my existing User Active Record Model, or should I create a separate Form Model for the User registration form?

1

There are 1 best solutions below

2
On BEST ANSWER

You can declare the variables in your model and then add rules for them using the scenario attribute as @Dan mentioned.

You model would look something like:

class User extends CActiveRecord
{
    public $password_confirm;
    public $password_hash;
    ...

and your rule in the model would look like:

array('password, password_confirm', 'required', 'on'=>'register')

and you also might want to use the CCompareValidator rule or similar to check the password fields match. See here.