5

I have a table named SYSTEM_PARAMS like below

+------------+------------------+------+-----+---------------------+-----------------------------+
| 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                   |                             |
+------------+------------------+------+-----+---------------------+-----------------------------+

where i have all names of the cron jobs i have to run and update the value against the specific job name with the current running JobId, the table does not have the primaryKey defined as you can see the schema above, so i defined the method primaryKey() inside the model like below

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

but this gives me error saying that i can define a static method as non-static , what am i doing wrong here.

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

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68

1 Answers1

12

Exactly what it says.

primaryKey is static method in ActiveRecord class.

If you want to override it you have to make it static as well.

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

PS. It must return array. See this note.

Bizley
  • 17,392
  • 5
  • 49
  • 59
  • hmm... but after defining it as static when i try to update the `value` field, it gives me another error PHP Warning 'yii\base\ErrorException' with message 'Invalid argument supplied for foreach()' in F:\xampp\htdocs\Nxb\sms\_protected\vendor\yiisoft\yii2\db\BaseActiveRecord.php:1098 – Muhammad Omer Aslam Jan 08 '17 at 17:07
  • i thought that might be because of the name of the column `value` so i renamed it to `job_id` , and still it gives me the same error on `$model->save()` – Muhammad Omer Aslam Jan 08 '17 at 17:13
  • 1
    Method must return array - is it in your case? – Bizley Jan 08 '17 at 17:16
  • you are right i had to return as an array ,, thanks for your help @Bizley – Muhammad Omer Aslam Jan 08 '17 at 17:17