public function _run($request, $xcontext) { $id = intval(Utils::testInput($request->id)); $this->logger->info("Input id: {$id}"); if (empty($id)) { $this->logger->error("Input id is empty!"); throw new Exception("need id!"); } $bookService = new BookService(); $bookList = $bookService->lists(); foreach ($bookList as $book) { if ($book->getAuthor()->id() == $id) { $nodeletion = "该作者与书籍关联,不能删除"; return XNext::action("listauthor", array("nodeletion" => $nodeletion)); } } $authorService = new AuthorService(); $author = $authorService->delete($id); $this->logger->info("Delete an author whose id is {$id}"); if (!$author || $author->id() <= 0) { $this->logger->error("failed in deleting author!"); throw new Exception("failed in deleting author!"); } return XNext::action("listauthor"); }
public function test_delete() { $as = XAppSession::begin(); $name = "testname_delete"; $authorService = new AuthorService(); $author = $authorService->add($name); $as->commit(); $this->logger->info("Author status before: " . $author->getStatus()); $this->assertTrue(!is_null($author) && $author->getStatus() == Constants::USING); $author = $authorService->delete($author->id()); $as->commit(); $this->logger->info("Author status after: " . $author->getStatus()); $this->assertTrue(!is_null($author) && $author->getStatus() == Constants::DELETED); Dwriter::ins()->del_Author_by_id($author->id()); $as->commit(); $as = null; }