When the user registers, I am able to insert into this table and generate an activation link that they must click to complete the registration process. The url I generate contains just the activation code.
In my activate controller I have this:
public function actionRegister() {
if (isset($_GET['activation_code'])) {
$code = $_GET['activation_code'];
$register = new Register('activate');
$register->attributes = array('activation_code'=>$code);
if ($register->validate()) {
$email = $register->email; <-- returning null
...
}
}
}
My question is how to force the AR to grab the email from the database? I don’t understand why it isn’t by default trying to get this value from the database? One way is to do an explicit find after validate, but I would prefer if I didn’t do any explicit sql.
You should retrieve a record from the db instead of creating a new instance of your model, don’t you think so?
public function actionRegister() {
if (isset($_GET['activation_code'])) {
$code = $_GET['activation_code'];
$register = Register::model()->findByAttributes(array('activation_code'=>$code));
if ($register != null) {
$email = $register->email; // will now return the associated email
...
}
}
}
By the way, using $_GET this way is bad practise and can lead to SQL injection attacks, you should do something like this:
public function actionRegister($activation_code=false) {
if ($activation_code) {
$register = Register::model()->findByAttributes(array('activation_code'=>$activation_code));
if ($register != null) {
$email = $register->email; // will now return the associated email
...
}
}
}
I would also say that generally POST should be used for anything which changes data on the server (I’m presuming here that you will be saving the validation state).