Blog example, extend User table with lastLogin DATETIME field

Hello,

I'm completely new to Yii and PHP. I have setup Yii and created a new project called MyFirstYii. I'm following the blog demo application to learn the Yii framework.

Now I'm working on the UserIdenty.php file. I want to extend the blog example with an additional field in the User table called lastLogin DATETIME.

And now I'm stuck. The example uses for all datetime fields in the database an integer. I do understand why this is done, it's easy and portable across all databases. But if you want to use external tools on the database, an integer won't be converted to a datetime.

Could someone please give me a coding example how to update the lastLogin field, as a DATETIME field, database independend ? And how should the reverse be coded, retrieving a DATETIME field and converting is to a PHP time ?

The code I have so far is:

class UserIdentity extends CUserIdentity

{

    private $_id;

   

    public function authenticate()

    {

  $username=strtolower($this->username);

        $user=User::model()->find('LOWER(username)=?',array($username));

        if($user===null)

        {

            $this->errorCode=self::ERROR_USERNAME_INVALID;

            return false;

        }

       

        if($this->password!==$user->password)

        {

            $this->errorCode=self::ERROR_PASSWORD_INVALID;

            return false;

        }

       

        $this->_id=$user->id;

        $this->username=$user->username;

        $this->errorCode=self::ERROR_NONE;

       

        //Update lastLogin

        //$currenttime = date( 'Y-m-d H:i:s', time());

        return true;

    }

Thanks in advance,

Mirco

You could do the following, assuming you need to deal with lastLogin field:



public function getLastLoginTimestamp()


{


    return strtotime($this->lastLogin);


}





public function setLastLoginTimestamp($value)


{


    $this->lastLogin=date(....,$value);


}


Then, in your code, you can access $model->lastLoginTimestamp instead of $model->lastLoginTime.

Thanks, this works.

public function setLastLoginTimestamp($value)

{

    $this->lastLogin=date('Y-m-d H:i:s',$value);

}

But I have some questions about the solution:

  1. The YIIC shell MODEL command imports a DATETIME field as a string. Is this solution database dependend ? (e.g. different date format necessary when using a different database)

  2. What about I18n and date formats ? (Is this date format fixed for all databases, or is this a MySql database solution ?)

Maybe these questions are more in the database area. I'm using MySql and I want to be able at any time to use another database, without having to modify one single letter of code.

Regards,

Mirco