protected function actionCreate() { try { $article = new \App\Models\News(); $article->fill([]); $article->save(); } catch (MultiException $e) { $this->view->errors = $e; } $this->view->display(__DIR__ . '/../templates/create.php'); }
protected function actionAdd() { $article = new \App\Models\News(); $article->title = $_REQUEST['title']; $article->intro_text = $_REQUEST['intro_text']; $article->full_text = $_REQUEST['full_text']; $article->author_id = 1; $article->created_at = date('Y-m-d H:i:s'); $article->save(); $this->view->message = "Новость добавлена"; $this->view->display(__DIR__ . '/../Views/admin_redirect.php'); }
protected function actionEdit() { $id = $_GET['id'] ?? false; if (false !== $id) { if (false === ($article = \App\Models\News::findByID($id))) { throw new \App\Exceptions\NotFound($_SERVER['REQUEST_URI']); } } else { $article = new \App\Models\News(); } if (!empty($_POST)) { try { $article->fillByPost($_POST); } catch (MultiException $e) { $this->view->display('edit.php', ['article' => $article, 'authors' => \App\Models\Author::findAll(), 'errors' => $e]); exit(0); } $this->redirectIf('/admin/news/index', $article->save()); } $this->view->display('edit.php', ['article' => $article, 'authors' => \App\Models\Author::findAll()]); }
protected function actionSave() { try { if (isset($_POST['id'])) { $news = \App\Models\News::findById($_POST['id']); if (!$news) { throw new \App\Exceptions\Err404('Новость не найдена '); } } else { $news = new \App\Models\News(); } $news->fill($_POST); $news->save(); header('Location: /admin/index'); exit; } catch (\Lib\MultiException $e) { $this->view->errors = $e; $logger = new \App\LogUseLib(); $logger->getArrMess($e); $this->view->news = $news; $this->view->display(__DIR__ . '/../templates/admin/edit.php'); } }
<?php require __DIR__ . '/autoload.php'; if (!empty($_POST['name'])) { if (!empty($_POST['id'])) { $article = \App\Models\News::findById((int) $_GET['id']); } else { $article = new \App\Models\News(); } $article->name = htmlspecialchars($_POST['name']); $article->preview_content = htmlspecialchars($_POST['preview_content']); $article->detail_content = htmlspecialchars($_POST['detail_content']); $article->date_create = htmlspecialchars($_POST['date_create']); $article->save(); if (!empty($_POST['save'])) { header('Location: /'); } } if (!empty($_GET['id'])) { $article = \App\Models\News::findById((int) $_GET['id']); if (!empty($_GET['del'])) { $article->delete(); header('Location: /'); } } else { $article = new \App\Models\News(); } include __DIR__ . '/views/article_edit.tpl';
<?php require __DIR__ . '/../../autoload.php'; $id = $_GET['id'] ?: false; if (!empty($id)) { if (!empty($article = App\Models\News::findById($id))) { include __DIR__ . '/../Views/article.php'; } else { echo 'Запись с таким id отсутствует'; } } else { header('Location: /'); }
$user = new User(); $user->name = 'Агрипина'; $user->email = '*****@*****.**'; echo 'Добавили: ' . $user->save() . '<br>'; echo 'id новой записи: ' . $user->id; echo '<hr>'; /** * Тестирование метода save() в классе Model => вызов update() */ $id = 3; $user = App\Models\User::findById($id); $user->name = 'Валерия'; $user->email = '*****@*****.**'; echo 'Изменена ли запись с id=' . $id . ': ' . $user->save(); echo '<hr>'; /** * Тестирование метода delete() в классе Model */ $id = 4; $user = App\Models\User::findById($id); echo 'Удалена ли запись с id=' . $id . ': ' . $user->delete(); echo '<hr>'; /** * Тестирование метода save() в классе Model (объект News) */ $id = 2; $news = App\Models\News::findById($id); $news->title = 'Исправленный заголовок'; $news->content = 'Исправленный контент'; echo 'Изменена ли запись с id=' . $id . ': ' . $news->save(); echo '<hr>';
public function run() { App\Models\News::truncate(); factory(App\Models\News::class, 20)->create(); }