'setPrimaryKeyValue', ]; } public function setPrimaryKeyValue(): bool { if ($this->owner->getPrimaryKey() === null) { $this->refreshPrimaryKeyValue(); } return true; } public function refreshPrimaryKeyValue(): void { do { $key = $this->generateValue(); } while ($this->isValueExists($key)); $this->owner->{$this->getPrimaryKeyName()} = $key; } protected function generateValue(): string { return (string)call_user_func($this->value); } protected function isValueExists(string $key): bool { $owner = $this->owner; return $owner::find()->andWhere([$this->getPrimaryKeyName() => $key])->exists(); } protected function getPrimaryKeyName(): string { $owner = $this->owner; $primaryKeys = $owner::primaryKey(); if (!isset($primaryKeys[0])) { throw new InvalidConfigException('"' . $owner::class . '" must have a primary key.'); } if (count($primaryKeys) > 1) { throw new InvalidConfigException('Current behavior don\'t support models with more then one primary key.'); } return $primaryKeys[0]; } }