Model Zend Framework 2模型外键

Model Zend Framework 2模型外键,model,zend-framework2,tablegateway,Model,Zend Framework2,Tablegateway,我现在正在接近Zend Framework 2,并遵循了这一点。一切都很好,但现在我想,例如,添加一个流派的专辑 我在数据库中添加了一个category表,并在相册表中创建了一个外键category\u id,它引用category表中的category.id 我如何使用TableGateway在我的模型上反映这种情况?我希望这将对您有所帮助,您将了解如何完成任务: <?php In module/Album/src/Album/Model/AlbumTable.php =========

我现在正在接近Zend Framework 2,并遵循了这一点。一切都很好,但现在我想,例如,添加一个流派的专辑

我在数据库中添加了一个category表,并在相册表中创建了一个外键
category\u id
,它引用category表中的
category.id


我如何使用
TableGateway
在我的模型上反映这种情况?

我希望这将对您有所帮助,您将了解如何完成任务:

<?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));
    }
}

谢谢@prava。这是一个切实可行的解决办法。但是补水剂呢?我阅读了在TableGateway和模型类之间使用的。你知道怎么把它们联系起来吗?是的,我知道。我会在几天后更新我的答案。谢谢,非常有帮助。抱歉,如果我一直在问,但是,如果我想将Category建模为一个类,该怎么办?抱歉,没有理解您的观点@Danielanzelmi,请简单描述一下:)我想将Category类注入相册,而不是添加两个字段(CategoryId和CategoryName)。类似于$album->setCategory(Category$Category);
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;
    }
}