36 lines
830 B
PHP
36 lines
830 B
PHP
|
<?php
|
||
|
namespace App\Data\Repository;
|
||
|
|
||
|
|
||
|
class BaseRepository {
|
||
|
protected $table = "";
|
||
|
|
||
|
public function update($obj) {
|
||
|
$pdo = app('db')->getPdo();
|
||
|
$sql = "UPDATE `".$this->table."` SET ";
|
||
|
$first = true;
|
||
|
|
||
|
$values = [];
|
||
|
foreach (get_object_vars($obj) as $key => $value) {
|
||
|
if(in_array($key, ["id", "created_at"])) {
|
||
|
continue;
|
||
|
}
|
||
|
if(!$first) {
|
||
|
$sql .= ", ";
|
||
|
}
|
||
|
$first = false;
|
||
|
$sql .= "`".$key."` = :".$key;
|
||
|
if(is_bool($value)) {
|
||
|
$value = (int)$value;
|
||
|
}
|
||
|
$values[":".$key] = $value;
|
||
|
}
|
||
|
$sql .= " WHERE `id` = ".$obj->id;
|
||
|
|
||
|
$sth = $pdo->prepare($sql);
|
||
|
$sth->execute($values);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
}
|