如何覆盖 primaryKey() 方法。在 Yii2 上

How to override the primaryKey() method. on Yii2

早上好亲爱的社区,

我是 Yii2 的新手,正在开发一个用户登录程序,但我收到一个数据库异常,为什么我尝试登录是因为用户的主键 table

这是我在 vendor/yiisoft/yii2/db/BaseActiveRecord 的代码。php

提前感谢您的宝贵帮助

     * 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;
        }```

对于将面临这种错误的每个人

这是解决方案

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