Unable To Use Login

I was doing ok learning Yii 1.1 and decided to try my hand at Yii2. I am having a basic problem with the login and get the following error using the basic template.

"Call to undefined method app\models\User::findByUsername()"

Is this a function I have to write myself (I can do that) or is there suppose to be something and I missed it when I setup the basic app. (It was my first time using Composer)

Thanks

That’s strange, because this function is already exist in basic app template. Make sure you haven’t deleted it.

https://github.com/yiisoft/yii2/blob/master/apps/basic/models/User.php

Also, you can take a look at advanced app example:

https://github.com/yiisoft/yii2/blob/master/apps/advanced/common/models/User.php

I agree, The model "User" that I have does not have that or FindByIdentity() in it. Here is what my User model has in it. (after running gii)


<?php


namespace app\models;

use yii\db\ActiveRecord;

use yii\helpers\SecurityHelper;

use yii\web\Identity;


/**

 * This is the model class for table "user".

 *

 * @property integer $id

 * @property string $fName

 * @property string $lName

 * @property string $email

 * @property string $address

 * @property string $city

 * @property string $state

 * @property string $password

 */

class User extends \yii\db\ActiveRecord

{

	/**

	 * @inheritdoc

	 */

	public static function tableName()

	{

		return 'user';

	}


	/**

	 * @inheritdoc

	 */

	public function rules()

	{

		return [

			[['id', 'fName', 'lName', 'email', 'password'], 'required'],

			[['id'], 'integer'],

			[['address', 'city'], 'string'],

			[['fName', 'lName'], 'string', 'max' => 30],

			[['email', 'password'], 'string', 'max' => 60],

			[['state'], 'string', 'max' => 10]

		];

	}


	/**

	 * @inheritdoc

	 */

	public function attributeLabels()

	{

		return [

			'id' => 'ID',

			'fName' => 'F Name',

			'lName' => 'L Name',

			'email' => 'Email',

			'address' => 'Address',

			'city' => 'City',

			'state' => 'State',

			'password' => 'Password',

		];

	}

        

}

I had not thought of looking at the file on Github as I assumed it would be the same as what I have. (I will not make that mistake again) Is it possible that I used composer wrong and got the wrong version of the basic app? Sorry, this is all new to me and I am slowly learning all the tools plus the language, plus the framework. (I miss the old days of classic ASP and Dreamweaver :)

Thanks so much for your help and time to respond!

No, I suppose you have overwritten User model by gii-generated one.

I don’t think it can be considered as a bug (gii is kinda straightforward), just make sure to re-implement missing features.

In particular,

class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface

and all the interface-related stuff (findIdentity … validatePassword).

You can use example from advanced app template as a reference.

And here are the docs for Yii2 auth: https://github.com/yiisoft/yii2/blob/master/docs/guide/authentication.md

I think as ORey mentioned you may have overwritten the default User model.

You can reinstall a fresh yii advanced app with composer and copy the findByUsername function from the User model.