'How to override the primaryKey() method. on Yii2

Good morning dear community,

I'm new to Yii2 and working on a user login program but i get a database Exception why i try to login due to the primary key for user table

Here my code at vendor/yiisoft/yii2/db/BaseActiveRecord.php

Thank you in advance for the valuable help

     * is composite or `$asArray` is `true`. A string is returned otherwise (null will be returned if
     * the key value is null).
     * @throws Exception if the AR model does not have a primary key
     */
    public function getOldPrimaryKey($asArray = false)
    {
        $keys = $this->primaryKey();
        if (empty($keys)) {
            throw new Exception(get_class($this) . ' does not have a primary key. You should either define a primary key for the corresponding table or override the primaryKey() method.');
        }
        if (!$asArray && count($keys) === 1) {
            return isset($this->_oldAttributes[$keys[0]]) ? $this->_oldAttributes[$keys[0]] : null;
        }
 
        $values = [];
        foreach ($keys as $name) {
            $values[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
        }```



Solution 1:[1]

For everybody that gonna face this kind of error

here is the solution

   public static function primaryKey()
    {
        return ["id"];
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dev-doul