This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccount/app/Data/Repository/BaseRepository.php
2019-12-02 14:26:32 +01:00

35 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;
}
}