yii2 更新没有主键的表

yii2 Updating Tables without primary key

我有一个名为 SYSTEM_PARAMS 的 table,如下所示

+------------+------------------+------+-----+---------------------+-----------------------------+
| Field      | Type             | Null | Key | Default             | Extra                       |
+------------+------------------+------+-----+---------------------+-----------------------------+
| name       | varchar(50)      | NO   |     | NULL                |                             |
| value      | varchar(100)     | YES  |     | NULL                |                             |
| updated_at | timestamp        | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |
| created_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                             |
| created_by | int(11)          | NO   |     | NULL                |                             |
| updated_by | int(11) unsigned | YES  |     | 0                   |                             |
+------------+------------------+------+-----+---------------------+-----------------------------+

我有我必须 运行 的 cron 作业的所有名称,并使用当前 运行ning JobId、[=23] 更新特定作业名称的值=] 没有定义 primaryKey 正如你可以看到上面的模式,所以我在模型中定义了方法 primaryKey() 如下

 public function primaryKey($asArray=FALSE) {
        return 'name';
    }

但这给了我一个错误,说我可以将静态方法定义为非静态方法,我在这里做错了什么。

PHP Fatal error: Cannot make static method yii\db\ActiveRecord::primaryKey() non static in class common\models\SystemParams

正如其名。

primaryKey 是 ActiveRecord 中的静态方法 class.

如果您想覆盖它,您也必须将其设为静态。

public static function primaryKey()
{
    return ['name'];
}

PS。它必须 return 数组。参见 this note