Unknown property while logging using own model

Hello.

Using my own database I’ve created a model. My DB is postgreSQL which looks like this:




CREATE TABLE "Uzytkownicy"

(

  id serial NOT NULL,

  uzytkownik character varying,

  haslo character varying,

  rola character varying,

  klucz character varying,

  imie character varying,

  nazwisko character varying,

  CONSTRAINT "Uzytkownicy_pkey" PRIMARY KEY (id )

)

WITH (

  OIDS=FALSE

);

ALTER TABLE "Uzytkownicy"

  OWNER TO yii;



and my model ‘Uzytkownicy’:




<?php


namespace app\models;


use Yii;


/**

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

 *

 * @property integer $id

 * @property string $uzytkownik

 * @property string $haslo

 * @property string $rola

 * @property string $klucz

 * @property string $imie

 * @property string $nazwisko

 */

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

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'Uzytkownicy';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['uzytkownik', 'haslo', 'rola', 'klucz', 'imie', 'nazwisko'], 'string']

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'uzytkownik' => 'Uzytkownik',

            'haslo' => 'Haslo',

            'rola' => 'Rola',

            'klucz' => 'Klucz',

            'imie' => 'Imie',

            'nazwisko' => 'Nazwisko',

        ];

    }


    public function getAuthKey() {

        return $this->klucz;

    }


    public function getId() {

        return $this->id;

    }


    public function validateAuthKey($authKey) {

        return $this->klucz === $authKey;

    }


    public static function findIdentity($id) {

        return self::findOne($id);

    }


    public static function findIdentityByAccessToken($token, $type = null) {

        throw new \yii\base\NotSupportedException;

    }

    

    public static function findByUsername($username) {

        return self::findOne(['uzytkownik'=>$username]);

    }

    

    public function validatePassword($password) {

        return $this->haslo === $password;

    }

}



When I’m trying to login, I’m receiving an exception:


Unknown Property – yii\base\UnknownPropertyException

Getting unknown property: app\models\Uzytkownicy::username


    1. in C:\inetpub\https\112\vendor\yiisoft\yii2\base\Component.php at line 143

    134135136137138139140141142143144145146147148149150151152


                foreach ($this->_behaviors as $behavior) {

                    if ($behavior->canGetProperty($name)) {

                        return $behavior->$name;

                    }

                }

            }

            if (method_exists($this, 'set' . $name)) {

                throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);

            } else {

                throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);

            }

        }

     

        /**

         * Sets the value of a component property.

         * This method will check in the following order and act accordingly:

         *

         *  - a property defined by a setter: set the property value

         *  - an event in the format of "on xyz": attach the handler to the event "xyz"


    2. in C:\inetpub\https\112\vendor\yiisoft\yii2\db\BaseActiveRecord.php at line 247 – yii\base\Component::__get('username')

    3. in C:\inetpub\https\112\views\layouts\main.php at line 48 – yii\db\BaseActiveRecord::__get('username')

    42434445464748495051525354


                Yii::$app->user->isGuest ? (

                    ['label' => 'Login', 'url' => ['/site/login']]

                ) : (

                    '<li>'

                    . Html::beginForm(['/site/logout'], 'post')

                    . Html::submitButton(

                        'Logout (' . Yii::$app->user->identity->username . ')',

                        ['class' => 'btn btn-link']

                    )

                    . Html::endForm()

                    . '</li>'

                )

            ],


    4. in C:\inetpub\https\112\vendor\yiisoft\yii2\base\View.php at line 325 – require('C:\inetpub\https\112\views\layou...')

    5. in C:\inetpub\https\112\vendor\yiisoft\yii2\base\View.php at line 247 – yii\base\View::renderPhpFile('C:\inetpub\https\112\views\layou...', ['content' => '<div class="site-index"> <d...'])

    6. in C:\inetpub\https\112\vendor\yiisoft\yii2\base\Controller.php at line 392 – yii\base\View::renderFile('C:\inetpub\https\112\views\layou...', ['content' => '<div class="site-index"> <d...'], app\controllers\SiteController)

    7. in C:\inetpub\https\112\vendor\yiisoft\yii2\base\Controller.php at line 378 – yii\base\Controller::renderContent('<div class="site-index"> <d...')

    8. in C:\inetpub\https\112\controllers\SiteController.php at line 52 – yii\base\Controller::render('index')

    46474849505152535455565758


                ],

            ];

        }

     

        public function actionIndex()

        {

            return $this->render('index');

        }

     

        public function actionLogin()

        {

            if (!\Yii::$app->user->isGuest) {

                return $this->goHome();


    9. app\controllers\SiteController::actionIndex()

    10. in C:\inetpub\https\112\vendor\yiisoft\yii2\base\InlineAction.php at line 55 – call_user_func_array([app\controllers\SiteController, 'actionIndex'], [])

    11. in C:\inetpub\https\112\vendor\yiisoft\yii2\base\Controller.php at line 154 – yii\base\InlineAction::runWithParams([])

    12. in C:\inetpub\https\112\vendor\yiisoft\yii2\base\Module.php at line 454 – yii\base\Controller::runAction('', [])

    13. in C:\inetpub\https\112\vendor\yiisoft\yii2\web\Application.php at line 84 – yii\base\Module::runAction('', [])

    14. in C:\inetpub\https\112\vendor\yiisoft\yii2\base\Application.php at line 375 – yii\web\Application::handleRequest(yii\web\Request)

    15. in C:\inetpub\https\112\web\index.php at line 12 – yii\base\Application::run()

    6789101112


     

    require('C:\inetpub\https\112\vendor\autoload.php');

    require('C:\inetpub\https\112\vendor\yiisoft\yii2\Yii.php');

     

    $config = require('C:\inetpub\https\112\config\web.php');

     

    (new yii\web\Application($config))->run();


$_COOKIE = [

    '_ga' => 'GA1.4.329081168.1439557110',

    '_csrf' => 'f9365217335a5b00470c97022cb546d615d9f78b8c6bb2c76ba78f4a30b58813a:2:{i:0;s:5:"_csrf";i:1;s:32:"7IP6_YdJqBY9gdFfiCyErTVTG-hulSzz";}',

    'PHPSESSID' => 'e6jngiqlahma1e6a35jpkcov66',

];


$_SESSION = [

    '__flash' => [],

    '__id' => 1,

];




It says, it’s getting unknown property: app\models\Uzytkownicy::username. But I haven’t defined it anywhere.

It says right there in the log (#3):

‘Logout (’ . Yii::$app->user->identity->username . ‘)’,

Change it to ‘uzytkownik’ or something.

Just to elaborate:

Yii::$app->user->identity is pointing to the model (the IdentityInterface) so you can’t use a field name that is not in the model.

username is not in your model. :)

Thank you, it works!