简体   繁体   English

Zend Framework 2 中的数据库访问

[英]Database access in Zend Framework 2

I want to find a good way to go about accessing my database from ZF2.我想找到一种从 ZF2 访问我的数据库的好方法。 I don't want to do this from my models or controllers and I don't want to use any ORM tools such as Doctrine (unless someone does a good job of convincing me!).我不想从我的模型或控制器中执行此操作,也不想使用任何 ORM 工具,例如 Doctrine(除非有人很好地说服了我!)。

The tutorial application is using the Table Data Gateway pattern; 教程应用程序使用表数据网关模式; it creates a class AlbumTable which accesses the album table in the database.它创建一个类AlbumTable来访问数据库中的album表。 I am concerned about this approach because I will be making a normalized database where I need to join several tables to represent some models.我担心这种方法,因为我将制作一个规范化的数据库,我需要在其中加入几个表来表示一些模型。 As far as I understand, this cannot be done with the TableGateway class.据我所知,这不能用TableGateway类来完成。 Sure, you could do $tblgateway->getSql() and go from there, but that kind of defeats the purpose of the Table Data Gateway pattern I think.当然,您可以执行$tblgateway->getSql()并从那里开始,但我认为这违背了表数据网关模式的目的。

So, I turned my attention to Zend\\Db\\Sql\\Sql .因此,我将注意力转向Zend\\Db\\Sql\\Sql Building on the Album example, I got the following working:以专辑示例为基础,我得到了以下工作:

class AlbumMapper {
    private $sql;

    public function __construct(Sql $sql) {
        $this->sql = $sql;
    }

    public function getAlbum($id) {
        $id = (int) $id;

        $select = $this->sql->select();
        $select->from('album');
        $select->where(array('id' => $id));

        $statement = $this->sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $row = $result->current();

        $album = null;

        if ($row) {
            $album = new Album();

            $album->setTitle($row['title']);
            $album->setArtist($row['artist']);
            $album->setId($row['id']);
        }

        return $album;
    }
}

The point here is that I could call $select->join() and join several tables, which would give me more control of querying my database and remove the 1:1 relationship between my models and the tables.这里的重点是我可以调用$select->join()并加入多个表,这将使我能够更好地控制查询我的数据库并删除我的模型和表之间的 1:1 关系。 However, if I do not need to join, then using the Table Data Gateway would surely save me from writing a bunch of code.但是,如果我不需要加入,那么使用表数据网关肯定可以让我免于编写一堆代码。

So, I guess my question is if a combination of the two approaches would be good depending on the situation?所以,我想我的问题是,根据具体情况,这两种方法的组合是否合适? For instance, if I only need to access a single table (no joining with others), then I could use the Table Data Gateway, and if I need to join several tables, I could go with the other approach.例如,如果我只需要访问一个表(不与其他人联接),那么我可以使用表数据网关,如果我需要联接多个表,我可以使用另一种方法。 Is this a good or bad idea?这是个好主意还是坏主意? Are there any better alternatives?有没有更好的选择?

Thank you in advance!先感谢您!

I use Mapper classes with Entity objects.我将 Mapper 类与实体对象一起使用。 To avoid writing too much repetitive code for the simple use-cases, I use ZfcBase's AbstractDbMapper to extends my specific mappers from.为了避免为简单的用例编写太多重复的代码,我使用 ZfcBase 的AbstractDbMapper来扩展我的特定映射器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM