SQL Abstraction and Object Hydration

In the last chapter, we introduced database abstraction and a new command interface for operations that might change what blog posts we store. We'll now start creating database-backed versions of the PostRepositoryInterface andPostCommandInterface, demonstrating usage of the various Zend\Db\Sql classes.

Preparing the Database

This tutorial assumes you've followed the Getting Started tutorial, and that you've already populated the data/zftutorial.db SQLite database. We will be re-using it, and adding another table to it.

Create the file data/posts.schema.sql with the following contents:

CREATE TABLE posts ( id INTEGER PRIMARY KEY AUTOINCREMENT, title varchar(100) NOT NULL, text TEXT NOT NULL);

INSERT INTO posts (title, text) VALUES  ('Blog #1',  'Welcome to my first blog post');
INSERT INTO posts (title, text) VALUES ('Blog #2', 'Welcome to my second blog post');
INSERT INTO posts (title, text) VALUES ('Blog #3', 'Welcome to my third blog post');
INSERT INTO posts (title, text) VALUES ('Blog #4', 'Welcome to my fourth blog post');
INSERT INTO posts (title, text) VALUES ('Blog #5', 'Welcome to my fifth blog post');

Now we will execute this against the existing data/zftutorial.db SQLite database using the sqlite command (or sqlite3; check your operating system):

$ sqlite data/zftutorial.db < data/posts.schema.sql

If you don't have a sqlite command, you can populate it using PHP. Create the following script in data/load_posts.php:

<?php
$db = new PDO('sqlite:' . realpath(__DIR__) . 'zftutorial.db');
$fh = fopen(__DIR__ . '/posts.schema.sql', 'r');
while ($line = fread($fh, 4096)) {
$line = trim($line);
$db->exec($line);
}
fclose($fh);

and execute it using:

$ php data/load_posts.php

Quick Facts Zend\Db\Sql

To create queries against a database using Zend\Db\Sql, you need to have a database adapter available. The "Getting Started" tutorial covered this in the database chapter, and we can re-use that adapter.

With the adapter in place and the new table populated, we can run queries against the database. The construction of queries is best done through the "QueryBuilder" features of Zend\Db\Sql which are Zend\Db\Sql\Sql for select queries, Zend\Db\Sql\Insert for insert queries, Zend\Db\Sql\Update for update queries and Zend\Db\Sql\Delete for delete queries. The basic workflow of these components is:

  1. Build a query using the relevant class: SqlInsertUpdate, or Delete.
  2. Create a SQL statement from the Sql object.
  3. Execute the query.
  4. Do something with the result.

Let's start writing database-driven implementations of our interfaces now.

Writing the repository implementation

Create a class named ZendDbSqlRepository in the Blog\Model namespace that implements PostRepositoryInterface; leave the methods empty for now:

// In module/Blog/src/Model/ZendDbSqlRepository.php:
namespace Blog\Model; use InvalidArgumentException;
use RuntimeException; class ZendDbSqlRepository implements PostRepositoryInterface
{
/**
* {@inheritDoc}
*/
public function findAllPosts()
{
} /**
* {@inheritDoc}
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function findPost($id)
{
}
}

Now recall what we have learned earlier: for Zend\Db\Sql to function, we will need a working implementation of the AdapterInterface. This is a requirement, and therefore will be injected using constructor injection. Create a __construct()method that accepts an AdapterInterface as its sole parameter, and stores it as an instance property:

// In module/Blog/src/Model/ZendDbSqlModel.php:
namespace Blog\Mapper; use InvalidArgumentException;
use RuntimeException;
use Zend\Db\Adapter\AdapterInterface; class ZendDbSqlRepository implements PostRepositoryInterface
{
/**
* @var AdapterInterface
*/
private $db; /**
* @param AdapterInterface $db
*/
public function __construct(AdapterInterface $db)
{
$this->db = $db;
} /**
* {@inheritDoc}
*/
public function findAllPosts()
{
} /**
* {@inheritDoc}
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function findPost($id)
{
}
}

Whenever we have a required parameter, we need to write a factory for the class. Go ahead and create a factory for our new repository implementation:

// In module/Blog/src/Factory/ZendDbSqlRepositoryFactory.php
namespace Blog\Factory; use Interop\Container\ContainerInterface;
use Blog\Model\ZendDbSqlRepository;
use Zend\Db\Adapter\AdapterInterface;
use Zend\ServiceManager\Factory\FactoryInterface; class ZendDbSqlRepositoryFactory implements FactoryInterface
{
/**
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return ZendDbSqlRepository
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new ZendDbSqlMapper($container->get(AdapterInterface::class));
}
}

We're now able to register our repository implementation as a service. To do so, we'll make two changes:

  • Register a factory entry for the new repository.
  • Update the existing alias for PostRepositoryInterface to point to the new repository.

Update module/Blog/config/module.config.php as follows:

return [
'service_manager' => [
'aliases' => [
// Update this line:
Model\PostRepositoryInterface::class => Model\ZendDbSqlRepository::class,
],
'factories' => [
Model\PostRepository::class => InvokableFactory::class,
// Add this line:
Model\ZendDbSqlRepository::class => Factory\ZendDbSqlRepositoryFactory::class,
],
],
'controllers' => [ /* ... */ ],
'router' => [ /* ... */ ],
'view_manager' => [ /* ... */ ],
];

With the adapter in place you're now able to refresh the blog index atlocalhost:8080/blog and you'll notice that the ServiceNotFoundException is gone and we get the following PHP Warning:

Warning: Invalid argument supplied for foreach() in {projectPath}/module/Blog/view/blog/list/index.phtml on line {lineNumber}

This is due to the fact that our mapper doesn't return anything yet. Let's modify the findAllPosts() function to return all blog posts from the database table:

// In /module/Blog/src/Model/ZendDbSqlRepository.php:
namespace Blog\Model; use InvalidArgumentException;
use RuntimeException
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Sql\Sql; class ZendDbSqlRepository implements PostRepositoryInterface
{
/**
* @var AdapterInterface
*/
private $db; /**
* @param AdapterInterface $db
*/
public function __construct(AdapterInterface $db)
{
$this->db = $db;
} /**
* {@inheritDoc}
*/
public function findAllPosts()
{
$sql = new Sql($this->dbAdapter);
$select = $sql->select('posts');
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute();
return $result;
} /**
* {@inheritDoc}
* @throws InvalidArgumentException
* @throw RuntimeException
*/
public function findPost($id)
{
}
}

Sadly, though, a refresh of the application reveals another error message:

PHP Fatal error:  Call to a member function getId() on array in {projectPath}/module/Blog/view/blog/list/index.phtml on line {lineNumber}

Let's not return the $result variable for now and do a dump of it to see what we get here. Change the findAllPosts() method and dump the result:

public function findAllPosts()
{
$sql = new Sql($this->dbAdapter);
$select = $sql->select('posts');
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute(); var_export($result);
die(); return $result;
}

Refreshing the application you should now see output similar to the following:

Zend\Db\Adapter\Driver\Pdo\Result::__set_state(array(
'statementMode' => 'forward',
'fetchMode' => 2,
'resource' =>
PDOStatement::__set_state(array(
'queryString' => 'SELECT "posts".* FROM "posts"',
)),
'options' => NULL,
'currentComplete' => false,
'currentData' => NULL,
'position' => -1,
'generatedValue' => '0',
'rowCount' =>
Closure::__set_state(array(
)),
))

As you can see, we do not get any data returned. Instead we are presented with a dump of some Result object that appears to have no data in it whatsoever. But this is a faulty assumption. This Result object only has information available for you when you actually try to access it. If you can determine that the query was successful, the best way to make use of the data within the Result object is to pass it to a ResultSet object.

First, add two more import statements to the class file:

use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\ResultSet;

Now update the findAllPosts() method as follows:

public function findAll()
{
$sql = new Sql($this->dbAdapter);
$select = $sql->select('posts');
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute(); if ($result instanceof ResultInterface && $result->isQueryResult()) {
$resultSet = new ResultSet();
$resultSet->initialize($result);
var_export($resultSet);
die();
} die('no data');
}

Refreshing the page, you should now see the dump of a ResultSet instance:

Zend\Db\ResultSet\ResultSet::__set_state(array(
'allowedReturnTypes' =>
array (
0 => 'arrayobject',
1 => 'array',
),
'arrayObjectPrototype' =>
ArrayObject::__set_state(array(
)),
'returnType' => 'arrayobject',
'buffer' => NULL,
'count' => NULL,
'dataSource' =>
Zend\Db\Adapter\Driver\Pdo\Result::__set_state(array(
'statementMode' => 'forward',
'fetchMode' => 2,
'resource' =>
PDOStatement::__set_state(array(
'queryString' => 'SELECT "album".* FROM "album"',
)),
'options' => NULL,
'currentComplete' => false,
'currentData' => NULL,
'position' => -1,
'generatedValue' => '0',
'rowCount' =>
Closure::__set_state(array(
)),
)),
'fieldCount' => 3,
'position' => 0,
))

Of particular interest is the returnType property, which has a value ofarrayobject. This tells us that all database entries will be returned as anArrayObject instances. And this is a little problem for us, as thePostRepositoryInterface requires us to return an array of Post instances. Luckily the Zend\Db\ResultSet subcomponent offers a solution for us, via theHydratingResultSet; this result set type will populate an object of a type we specify with the data returned.

Let's modify our code. First, remove the following import statement from the class file:

use Zend\Db\ResultSet\ResultSet;

Next, we'll add the following import statements to our class file:

use Zend\Hydrator\Reflection as ReflectionHydrator;
use Zend\Db\ResultSet\HydratingResultSet;

Now, update the findAllPosts() method to read as follows:

public function findAllPosts()
{
$sql = new Sql($this->db);
$select = $sql->select('posts');
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute(); if (! $result instanceof ResultInterface || ! $result->isQueryResult()) {
return [];
} $resultSet = new HydratingResultSet(
new ReflectionHydrator(),
new Post('', '')
);
$resultSet->initialize($result);
return $resultSet;
}

We have changed a couple of things here. First, instead of a normal ResultSet, we are now using the HydratingResultSet. This specialized result set requires two parameters, the second one being an object to hydrate with data, and the first one being the hydrator that will be used (a hydrator is an object that will transform an array of data into an object, and vice versa). We useZend\Hydrator\Reflection here, which is capable of injecting private properties of an instance. We provide an empty Post instance, which the hydrator will clone to create new instances with data from individual rows.

Instead of dumping the $result variable, we now directly return the initializedHydratingResultSet so we can access the data stored within. In case we get something else returned that is not an instance of a ResultInterface, we return an empty array.

Refreshing the page you will now see all your blog posts listed on the page. Great!

Refactoring hidden dependencies

There's one little thing that we have done that's not a best-practice. We use both a hydrator and an Post prototype inside our ZendDbSqlRepository. Let's inject those instead, so that we can reuse them between our repository and command implementations, or vary them based on environment. Update yourZendDbSqlRepository as follows:

// In module/Blog/src/Model/ZendDbSqlRepository.php:
namespace Blog\Model; use InvalidArgumentException;
use RuntimeException;
// Replace the import of the Reflection hydrator with this:
use Zend\Hydrator\HydratorInterface;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Db\Sql\Sql; class ZendDbSqlRepository implements PostRepositoryInterface
{
/**
* @var AdapterInterface
*/
private $db; /**
* @var HydratorInterface
*/
private $hydrator; /**
* @var Post
*/
private $postPrototype; public function __construct(
AdapterInterface $db,
HydratorInterface $hydrator,
PostInterface $postPrototype
) {
$this->db = $db;
$this->hydrator = $hydrator;
$this->postPrototype = $postPrototype;
} /**
* Return a set of all blog posts that we can iterate over.
*
* Each entry should be a Post instance.
*
* @return Post[]
*/
public function findAllPosts()
{
$sql = new Sql($this->db);
$select = $sql->select('posts');
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute(); if (! $result instanceof ResultInterface || ! $result->isQueryResult()) {
return [];
} $resultSet = new HydratingResultSet($this->hydrator, $this->postPrototype);
$resultSet->initialize($result);
return $resultSet;
} /**
* Return a single blog post.
*
* @param int $id Identifier of the post to return.
* @return Post
*/
public function findPost($id)
{
}
}

Now that our repository requires more parameters, we need to update theZendDbSqlRepositoryFactory and inject those parameters:

// In /module/Blog/src/Factory/ZendDbSqlRepositoryFactory.php
namespace Blog\Factory; use Interop\Container\ContainerInterface;
use Blog\Model\Post;
use Blog\Model\ZendDbSqlRepository;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Hydrator\Reflection as ReflectionHydrator;
use Zend\ServiceManager\Factory\FactoryInterface; class ZendDbSqlRepositoryFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new ZendDbSqlRepository(
$container->get(AdapterInterface::class),
new ReflectionHydrator(),
new Post('', '')
);
}
}

With this in place you can refresh the application again and you'll see your blog posts listed once again. Our repository no longer has hidden dependencies, and works with a database!

Finishing the repository

Before we jump into the next chapter, let's quickly finish the repository implementation by completing the findPost() method:

public function findPost($id)
{
$sql = new Sql($this->db);
$select = $sql->select('posts');
$select->where(['id = ?' => $id]); $statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute(); if (! $result instanceof ResultInterface || ! $result->isQueryResult()) {
throw new RuntimeException(sprintf(
'Failed retrieving blog post with identifier "%s"; unknown database error.',
$id
));
} $resultSet = new HydratingResultSet($this->hydrator, $this->postPrototype);
$resultSet->initialize($result);
$post = $resultSet->current(); if (! $post) {
throw new InvalidArgumentException(sprintf(
'Blog post with identifier "%s" not found.',
$id
));
} return $post;
}

The findPost() function looks similar to the findAllPosts() method, with several differences.

  • We need to add a condition to the query to select only the row matching the provided identifier; this is done using the where() method of the Sql object.
  • We check if the $result is valid, using isQueryResult(); if not, an error occurred during the query that we report via a RuntimeException.
  • We pull the current() item off the result set we create, and test to make sure we received something; if not, we had an invalid identifier, and raise anInvalidArgumentException.

Conclusion

Finishing this chapter, you now know how to query for data using theZend\Db\Sql classes. You have also learned a little about the zend-hydrator component, and the integration zend-db provides with it. Furthermore, we've continued demonstrating dependency injection in all aspects of our application.

In the next chapter we'll take a closer look at the router so we'll be able to start displaying individual blog posts.

SQL Abstraction and Object Hydration的更多相关文章

  1. 关于oracle PL/SQL存储过程 PLS-00905 object is invalid,statement ignored问题的解决

    昨天在学习oracle存储过程的时候,写了一个存储过程的demo,语句是这样的: )) AS psssal TESTDELETE.TESTID%TYPE; BEGIN SELECT TESTID IN ...

  2. sql server sys.object表字段说明

    列名 数据类型 说明 name sysname 对象名. object_id int 对象标识号. 在数据库中是唯一的. principal_id int 如果不是架构所有者,则为单个所有者的 ID. ...

  3. 分享公司DAO层动态SQL的一些封装

    主题 公司在DAO层使用的框架是Spring Data JPA,这个框架很好用,基本不需要自己写SQL或者HQL就能完成大部分事情,但是偶尔有一些复杂的查询还是需要自己手写原生的Native SQL或 ...

  4. string.Format , object[] args 使用

    string sql = "insert into TableA values('{0}','{1}','{2}',GetDate(),'{3}' "; sql = string. ...

  5. 简单数据访问类,生成简单SQL,自动转换成java对象

    import java.util.HashMap; import java.util.List; import java.util.Map; import org.slf4j.Logger; impo ...

  6. Android 连接 SQL Server (jtds方式)——下

    本文主要补充介绍jtds的查询方法,将以博主的一个实际开发程序进行说明 下图是项目的文件列表与界面效果:          运行效果: 1.三个EditText对应的是单个计划的序号.品种名.数量 2 ...

  7. SQL参数化

    本文来自:caodonglin 一.SQL参数化为什么能防注入? 因为执行计划被重用了,所以可以防SQL注入. 下面有两段SQL     正常SQL: 1 select COUNT(1) from C ...

  8. 20141129 LinQ to SQL

    ORMO-Object对象R-Relation关系M-Mapping映射 对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是 ...

  9. MyBatis7:MyBatis插件及示例----打印每条SQL语句及其执行时间

    Plugins 摘一段来自MyBatis官方文档的文字. MyBatis允许你在某一点拦截已映射语句执行的调用.默认情况下,MyBatis允许使用插件来拦截方法调用 Executor(update.q ...

随机推荐

  1. 【转】parallels desktop 11 授权许可文件删除方法

    原文网址:http://www.macappstore.net/tips/parallels-desktop-uninstall/ 很多同学在安装parallels desktop 11破解版后显示还 ...

  2. android Bitmap getByteCount和getRowBytes

    今天做图像缓存需要计算Bitmap的所占的内存空间,于是研究了下Bitmap关于内存占用的API 1.getRowBytes:Since API Level 1,用于计算位图每一行所占用的内存字节数. ...

  3. java中的快捷键

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...

  4. 消息系统Kafka介绍

    1.  概述 Kafka是Linkedin于2010年12月份开源的消息系统,它主要用于处理活跃的流式数 据.活跃的流式数据在web网站应用中非常常见,这 些数据包括网站的pv.用户访问了什么内容,搜 ...

  5. 仿酷狗音乐播放器开发日志三——修复CEditUI的bug2

    无意中发现了CEditUI控件的另一个bug,当我给播放器的搜索栏获取焦点时,这时再改变窗体大小,原本搜索栏应该对应着也改变大小,却发现CEditUI内嵌的edit控件没有跟着改变(如下图),跟着调试 ...

  6. 设计模式_Strategy_策略模式

    形象例子: 跟不同类型的MM约会,要用不同的策略,有的请电影比较好,有的则去吃小吃效果不错,有的去海边浪漫最合适,单目的都是为了得到MM的芳心,我的追MM锦囊中有好多Strategy哦.策略模式: 策 ...

  7. 【转载】grep,egrep,fgrep详解

    [转载自]http://blog.csdn.net/homking/article/details/6000711 egrep 等价于 grep -E fgrep 等价于 grep -F grep - ...

  8. DLLImport

    namespace Wintellect.Interop.Sound { using System; using System.Runtime.InteropServices; using Syste ...

  9. 拉格朗日对偶(Lagrange duality)

    拉格朗日对偶(Lagrange duality) 存在等式约束的极值问题求法,比如下面的最优化问题:              目标函数是f(w),下面是等式约束.通常解法是引入拉格朗日算子,这里使用 ...

  10. openstack fe