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

36 lines
830 B
PHP
Raw Normal View History

2019-12-02 13:26:32 +00:00
<?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;
}
}