robsonvleite / datalayer

The data layer is a persistent abstraction component of your database that PDO (O data layer é um componente para abstração de persistência no seu banco de dados que usa PDO com prepared statements)
https://www.upinside.com.br
MIT License
144 stars 53 forks source link

Added columns attribute, to easily access to the table columns. #26

Closed julianjedi closed 3 years ago

julianjedi commented 4 years ago

This feature allow us to easily create an autofill method for our models, just like in Laravel $model->fill($request->all());

namespace Source\Models;
use CoffeeCode\DataLayer\DataLayer;
class Model extends DataLayer

...

public function fill(array $data) : void
    {
        if($this->timestamps){
            $this->guarded = ['created_at', 'updated_at'];
        }
        $this->columns = array_diff($this->columns, $this->guarded) ?? [];
        foreach ($this->columns as $col)        {
            if(isset($data[$col]) && !empty($data[$col]))
            {
                $this->$col = $data[$col] ?? "";
            }  
        }
    }