【问题标题】:Zend Framework 2 model foreign keysZend Framework 2 模型外键
【发布时间】:2014-08-07 14:52:17
【问题描述】:

我现在正在接近 Zend Framework 2,我关注了 the tutorial on creating a simple CRUD application。一切都很好,但现在我想为专辑添加一个流派。

我在数据库中添加了一个类别表,并在专辑表中创建了一个外键category_id,它引用了类别表中的category.id

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

【问题讨论】:

    标签: model zend-framework2 tablegateway


    【解决方案1】:

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

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

    【讨论】:

    • 感谢@prava。这是一个实用的解决方案。但是保湿剂呢?我读到了在 TableGateway 和 Model 类之间使用的内容。你知道如何链接它们吗?
    • 是的,我知道。稍后我会更新我的答案。
    • 谢谢,很有帮助。对不起,如果我一直问,但是,如果我希望 Category 被建模为一个类怎么办?
    • 对不起,没有明白你的意思@DanieleAnzelmi,请简单描述一下:)
    • 我想将 Category 类注入相册,而不是添加两个字段(CategoryId 和 CategoryName)。类似 $album->setCategory(Category $category);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    相关资源
    最近更新 更多