**添加命令抽象**
> module/Blog/src/Model/PostCommandInterface.php
~~~
namespace Blog\Model;
interface PostCommandInterface
{
/**
* Persist a new post in the system.
*
* @param Post $post The post to insert; may or may not have an identifier.
* @return Post The inserted post, with identifier.
*/
public function insertPost(Post $post);
/**
* Update an existing post in the system.
*
* @param Post $post The post to update; must have an identifier.
* @return Post The updated post.
*/
public function updatePost(Post $post);
/**
* Delete a post from the system.
*
* @param Post $post The post to delete.
* @return bool
*/
public function deletePost(Post $post);
}
~~~
### 准备数据库
> data/posts.schema.sql
>
~~~
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');
~~~
执行命令导入数据
~~~
sqlite3 data/zftutorial.db < data/posts.schema.sql
~~~
### 写个仓库实现类
> module/Blog/src/Model/ZendDbSqlRepository.php
~~~
namespace Blog\Model;
use InvalidArgumentException;
use RuntimeException;
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,
Post $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)
{
$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;
}
}
~~~
**仓库工厂**
> 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('', '')
);
}
}
~~~
### 模块配置
> module/Blog/config/module.config.php
~~~
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' => [ /* ... */ ],
];
~~~