some questions...

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

Note:  of course I can do what I need to do without these features, just wondering if they exist

The reason for User::model()->find() instead of User::find() is because late static binding is only supported by PHP 5.3+. That is, when you call User::find(), AR won't be able to know what is the actual class unless you are using 5.3+.

Understanding this concept, we can classify methods of CActiveRecord into two categories: one is class-level methods which are very similar to static methods; one is instance-level methods such as save().

So you can use both User::model()->find() and $user->find(), but the former is recommended.

No, AR doesn't support using $user->find() to populate $user itself.

The closest method to your need is findByAttributes().

Ah, I see now.  So I'll keep using findByAttributes().  Thanks for explanation!

j