I have a user recovery form, where you can enter an email and it will send the login credentials to the entered email.
Here's the logic:
<?php
public function actionRecover() {
$user = new User;
if (isset($_POST['User'])) {
$user->setAttributes($_POST['User']);
if ($user->validate('recover')) {
//query by email
$found = User::model()->findByAttributes(array('email'=>$user->email));
if ($found !== null) {
echo $found->username;
//send email code here
Yii::app()->user->setFlash('recover',"An email has been sent to {$user->email}. Please check your email.");
} else {
$user->addError('email','Email not found');
}
}
}
$this->render('recover',array('user' => $user));
}
First it loads the email attribute from the form into the user model, then it validates it.
I noticed there are two ways of performing finds:
User::model()->find(…)
and
$user->find(…)
I'm confused about the differences of the two.
Is it possible to do something like
$user->find(…)
and then the results of the find are just put directly into the calling model instead of returned as a new model? Creating a whole new model does not make sense in this case speed wise.
Also, are there any find methods that simply build a find statement based on what attributes are already set in the model?
So something like this would be possible:
$model->email='a@b.c';
$model->find();
//now the the rest of the model attributes are populated from the database
I was not able to find anything like this, and am not sure if this is even logical for Yii.
Thanks,
Jonah