32], [['file_path'], 'string', 'max' => 500], ]; } public function beforeSave($insert) { if (!parent::beforeSave($insert)) { return false; } $now = date('Y-m-d H:i:s'); if ($insert && empty($this->created_at)) { $this->created_at = $now; } if (empty($this->source_type)) { $this->source_type = 'upload'; } $this->updated_at = $now; return true; } public function getAnimal() { return $this->hasOne(Animal::class, ['id' => 'animal_id']); } public function getFile() { return $this->hasOne(File::class, ['id' => 'file_id']); } public function getImageUrl(): string { $path = trim((string)$this->file_path); if ($path !== '') { if (preg_match('/^https?:\/\//i', $path) || substr($path, 0, 1) === '/') { return $path; } return '/' . ltrim($path, '/'); } if ($this->file_id) { $file = $this->file; if ($file instanceof File) { return (string)$file->getUrl([], false); } } return ''; } }