我现在正在接近 Zend Framework 2,并按照教程创建了一个简单的 CRUD 应用程序。一切都很好,但现在我想为专辑添加一个流派。
我在数据库中添加了一个类别表,并在专辑表中创建了一个外键category_id
,它引用category.id
了类别表中的 。
我如何在我的模型上反映这种情况TableGateway
?
我现在正在接近 Zend Framework 2,并按照教程创建了一个简单的 CRUD 应用程序。一切都很好,但现在我想为专辑添加一个流派。
我在数据库中添加了一个类别表,并在专辑表中创建了一个外键category_id
,它引用category.id
了类别表中的 。
我如何在我的模型上反映这种情况TableGateway
?
I hope this will help you and you will get the idea how to accomplish your task:
<?php
In module/Album/src/Album/Model/AlbumTable.php
==============================================
namespace Album\Model;
use Zend\Db\TableGateway\TableGateway;
class AlbumTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
$this->dbSql = new Sql($this->tableGateway->getAdapter()); // add this line
}
public function fetchAll()
{
$select = $this->dbSql->select();
$select->from('album')
->columns(array('id', 'artist', 'title'))
->join(array('C' => 'category'),
'fk_category = id_category',
array('id_category', 'name'),
$select::JOIN_LEFT);
$resultSet = $this->tableGateway->selectWith($this->select);
return $resultSet;
}
public function getAlbum($id)
{
$id = (int) $id;
$select = $this->dbSql->select();
$where = new Where();
$select->from('album')
->columns(array('id_album', 'artist', 'title'))
->join(array('C' => 'category'),
'fk_category = id_category',
array('id_category', 'name'),
$select::JOIN_LEFT);
$where->equalTo('id' => $id);
$rowset = $this->tableGateway->selectWith($this->select->where($where));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
public function saveAlbum(Album $album)
{
$data = array(
'artist' => $album->artist,
'title' => $album->title,
'fk_category' => $album->category_id
);
$id = (int)$album->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getAlbum($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Form id does not exist');
}
}
}
public function deleteAlbum($id)
{
$this->tableGateway->delete(array('id' => $id));
}
}
In module/Album/src/Album/Model/Album.php
=========================================
namespace Album\Model;
class Album
{
public $albumId;
public $artist;
public $title;
public $categoryId;
public $categoryName;
public function exchangeArray($data)
{
$this->albumId = (isset($data['id_album'])) ? $data['id_album'] : null;
$this->artist = (isset($data['artist'])) ? $data['artist'] : null;
$this->title = (isset($data['title'])) ? $data['title'] : null;
$this->categoryId = (isset($data['id_category'])) ? $data['id_category'] : null;
$this->categoryName = (isset($data['name'])) ? $data['name'] : null;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
}
In module/Album/src/Album/Factory/Model/AlbumTableFactory.php
=============================================================
namespace Album\Factory\Model;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Db\TableGateway\TableGateway;
use Album\Model\AlbumTable;
use Album\Model\Album;
use Zend\Stdlib\Hydrator\ObjectProperty;
use Zend\Db\ResultSet\HydratingResultSet;
class AlbumTableFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$db = $serviceLocator->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new HydratingResultSet();
$resultSetPrototype->setHydrator(new ObjectProperty());
$resultSetPrototype->setObjectPrototype(new Album());
$tableGateway = new TableGateway('album', $db, null, $resultSetPrototype);
$table = new AlbumTable($tableGateway);
return $table;
}
}