學習Zend3框架。處理一個樣例Zend3項目,我必須創建一個Model子類型。Zend3模型子類型
我有以下book.php中型號:
<?php
namespace Products\Model;
class Book
{
public $id;
public $author;
public $title;
public $isbn;
public function exchangeArray(array $data)
{
$this->id = !empty($data['id']) ? $data['id'] : null;
$this->author = !empty($data['author']) ? $data['author'] : null;
$this->title = !empty($data['title']) ? $data['title'] : null;
$this->isbn = !empty($data['isbn']) ? $data['isbn'] : null;
}
public function getArrayCopy()
{
return [
'id' => $this->id,
'author' => $this->author,
'title' => $this->title,
'isbn' => $this->isbn,
];
}
}
現在我要創建:
一個子類「圖書」,這被稱爲「驚悚」,從「尚書」繼承和所稱的「excitement_factor」
而一個附加屬性:
它正確地反映數據庫以及你的模型這種關係,我應該:
確保數據庫的完整性也終於:
實施適當的表格模型函數用於檢索,保存和刪除「驚悚片」 S,使用ORM風格的所有查詢
我怎樣才能做到這一點?
應該讓驚悚片延長書本類與新領域?我如何在Zend3中做到這一點?底層表呢? Will Zend會爲驚悚片創建新表嗎?我必須調整哪些文件?
ORM樣式方法是指fetchAll(),fetchOneBy()等?
感謝,
更新1:
的My Book表如下:
CREATE TABLE book (id INT AUTO_INCREMENT NOT NULL, author varchar(100) NOT NULL, title varchar(100) NOT NULL, isbn varchar(100) NOT NULL, type ENUM("Thriller") NOT NULL, PRIMARY KEY(id))
創建第二個表驚悚:
CREATE TABLE thriller
(
id INT NOT NULL,
book INT NOT NULL,
excitement_factor VARCHAR(100) NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(book) REFERENCES book(id)
)
兩者都有TableGateways。
型號均低於:
cat Book.php
<?php
namespace Products\Model;
class Book
{
public $id;
public $author;
public $title;
public $isbn;
private $inputFilter;
public function exchangeArray(array $data)
{
$this->id = !empty($data['id']) ? $data['id'] : null;
$this->author = !empty($data['author']) ? $data['author'] : null;
$this->title = !empty($data['title']) ? $data['title'] : null;
$this->isbn = !empty($data['isbn']) ? $data['isbn'] : null;
}
public function getArrayCopy()
{
return [
'id' => $this->id,
'author' => $this->author,
'title' => $this->title,
'isbn' => $this->isbn,
];
}
}
cat BookTable.php
<?php
namespace Products\Model;
use RuntimeException;
use Zend\Db\TableGateway\TableGatewayInterface;
class BookTable
{
private $tableGateway;
public function __construct(TableGatewayInterface $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
return $this->tableGateway->select();
}
public function getBook($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(['id' => $id]);
$row = $rowset->current();
if (! $row) {
throw new RuntimeException(sprintf(
'Could not find row with identifier %d',
$id
));
}
return $row;
}
public function saveBook(Book $book)
{
$data = [
'author' => $book->author,
'title' => $book->title,
'isbn' => $book->isbn,
];
$id = (int) $book->id;
if ($id === 0) {
$this->tableGateway->insert($data);
return;
}
if (! $this->getBook($id)) {
throw new RuntimeException(sprintf(
'Cannot update book with identifier %d; does not exist',
$id
));
}
$this->tableGateway->update($data, ['id' => $id]);
}
public function deleteBook($id)
{
$this->tableGateway->delete(['id' => (int) $id]);
}
}
cat Thriller.php
<?php
namespace Products\Model;
class Thriller
{
public $id;
public $book;
public $excitement_factor;
private $inputFilter;
public function exchangeArray(array $data)
{
$this->tid = !empty($data['id']) ? $data['id'] : null;
$this->book = !empty($data['book']) ? $data['book'] : null;
$this->excitement_factor = !empty($data['excitement_factor']) ? $data['excitement_factor'] : null;
}
public function getArrayCopy()
{
return [
'id' => $this->id,
'book' => $this->book,
'excitement_factor' => $this->excitement_factor,
];
}
}
cat ThrillerTable.php
<?php
namespace Products\Model;
use RuntimeException;
use Zend\Db\TableGateway\TableGatewayInterface;
class ThrillerTable
{
private $tableGateway;
public function __construct(TableGatewayInterface $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
return $this->tableGateway->select();
}
public function getThriller($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(['id' => $id]);
$row = $rowset->current();
if (! $row) {
throw new RuntimeException(sprintf(
'Could not find row with identifier %d',
$id
));
}
return $row;
}
public function saveThriller(Thriller $thriller)
{
$data = [
'book' => $thriller->book,
'excitement_factor' => $thriller->excitement_factor,
];
$id = (int) $thriller->id;
if ($id === 0) {
$this->tableGateway->insert($data);
return;
}
if (! $this->getThriller($id)) {
throw new RuntimeException(sprintf(
'Cannot update thriller with identifier %d; does not exist',
$id
));
}
$this->tableGateway->update($data, ['id' => $id]);
}
public function deleteThriller($id)
{
$this->tableGateway->delete(['id' => (int) $id]);
}
}
在我的控制器中加入書,我簡單地做:
public function addBookAction()
{
$form = new BookForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if (! $request->isPost()) {
return ['form' => $form];
}
$bookForm = new BookForm();
$form->setInputFilter($bookForm->getInputFilter());
$form->setData($request->getPost());
if (! $form->isValid()) {
return ['form' => $form];
}
$bookModel = new Book();
$bookModel->exchangeArray($form->getData());
$this->book->saveBook($bookModel);
return $this->redirect()->toRoute('product');
}
我現在該如何加驚悚?
public function addThrillerAction()
{
// ???
}
zf有關於創建具有CRUD操作的模塊的教程。這裏你是文檔鏈接。 https://docs.zendframework.com/tutorials/ –
謝謝。我知道如何創建模塊並使用TableGateway將模型綁定到數據庫。但我不明白我應該如何解決子類型的創建?你知道使用子類型的任何資源/示例應用程序嗎? –
與純php相同。只是擴展它們。什麼讓你多想一想? –