так то User у вас не модель, а репозиторий:
namespace Repository;
class User {
protected $db;
public function __construct(Adapter $db) {
$this->db = $db;
}
public function getUserById($id) {
$sql = "SELECT * FROM user WHERE id = :id";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch();
}
public function getImageById($id) {
$sql = "SELECT IF(image IS NULL or image = '','no_image.png',image) as image FROM `user` WHERE id = :id";
$result = $this->db->prepare($sql);
$result->bindParam(':id', $id, PDO::PARAM_INT);
$result->execute();
return $result->fetchColumn();
}
public function updateImageById($imageName, $id) {
$sql = "UPDATE user SET image = '$imageName' WHERE id = $id";
return $this->db->query($sql);
}
}
для работы с изображениями я предпочел бы сервис:
namespace Service;
class Image {
protected $userRepo;
function __construct(Repository\User $userRepo) {
$this->userRepo = $userRepo;
}
public function UploadImage(array $image) {
$usersImage = $image['userfile']['tmp_name'];
$imageName = $image['userfile']['name'];
if (is_uploaded_file($image['userfile']['tmp_name'])) {
move_uploaded_file($usersImage, $_SERVER['DOCUMENT_ROOT'] . "/template/images/users/" . $image['userfile']['name']);
}
return $imageName;
}
public function updateImage(array $image, $id) {
if($imageName = $this->uploadImage($image, $id)) {
$this->userRepo->updateImageById($imageName, $id);
return true;
}
}
}
а теперь самое интересное контроллер:
class UserController extends BaseController {
protected $imageService;
protected $userRepo;
function __construct(Service\Image $imageService, Repository\User $userRepo) {
$this->imageService = $imageService;
$this->userRepo = $userRepo;
}
public function actionProfile($id) {
if (isset($_POST['submit_photo'])) {
$this->imageService->updateImage($_FILES, $id);
}
return $this->render('user/profile.php', [
'user' => $this->userRepo->getUserById($id),
'image' => $this->userRepo->getImageById($id)
]);
}
}