<?php

namespace App\FormModel\admin\articles\trait;

use App\Model\AppArticle;

trait ModifyForUpdate
{
    protected array $canUpdateFields = ['title', 'cover', 'images', 'brand', 'location'];

    public function update()
    {
        $query = AppArticle::where(['aid' => $this->attributes['aid']])->first();
        foreach ($this->canUpdateFields as $field) {
            $val = $this->attributes[$field] ?? null;
            if ($val !== null) {
                $query->{$field} = $this->attributes[$field];
            }
        }

        $query->save();
    }
}