[SOLVED] Max datetime entry

I’ve just started using Yii this week and I’m trying to display the last time a user logged in by displaying the last successful entry in my login log.

From UserIdentity::authenticate():


$this->setState('lastLoginTime', $log->lastLoginTime($this->_id));

Which calls this function:

public function lastLoginTime($id){

			$sql = "SELECT MAX(Date_Created) FROM Login_Log WHERE Contact_ID=:contact AND Is_Successful=1;";

			$parameters = array(":contact"=>$id);


			return $lastLoginTime;


Once logged in, I want to display the last successful login on the homepage:

You last logged in on <?php echo Yii::app()->user->lastLoginTime; ?>

But all that appears is a boolean, I assume, since it only shows up with -1 .

the code looks right



echo $log->lastLoginTime($this->_id);


and see what it prints, maybe some errors in your sql sintax

What prints is:

"Object of class LoginLog could not be converted to string"

the data type for the Date_Created field is datetime, and I’m using SQL Server 2008 if that helps…

Should I not be using Yii::app()->db->createCommand()? Because when I insert the exact same query into sql server, it works.

I edited the code above to a better one. Try now and see what you get.

It should not be -1 in any case (false==0,true==1,null==’’)

:S It still echoes -1.

Even if I change the query to something like:

         $sql = "SELECT Date_Created FROM Login_Log WHERE ID=:contact";

         $parameters = array(":contact"=>15);


It will echo -1.

if you want to fetch the result use query instead of execute



EDIT: I haven’t notice that before

Thanks. I used queryRow() and it worked:

        $lastLoginTime = Yii::app()->db->createCommand()



		->where('Contact_ID=:id AND Is_Successful=1', array(':id'=>$id))



	return $lastLoginTime[''];