定制 coderxlsn/phly-mongo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

coderxlsn/phly-mongo

Composer 安装命令:

composer require coderxlsn/phly-mongo

包简介

ZF2 Module providing services and paginators surrounding MongoDB

README 文档

README

Build Status

PhlyMongo provides the following to assist with Mongo usage in ZF2:

  • Hydrating Mongo resultset
  • Mongo paginator adapter
  • Mongo paginator adapter for hydrating resultsets
  • Configurable service factories for the Mongo, MongoDB, and MongoCollection classes

Installation

Simplest is to add the following to composer.json:

{
    "minimum-stability": "dev",
    "require": {
        "coderxlsn/phly-mongo": "~1.0-dev"
    }
}

And then run:

php composer.phar install

Alternately, use git to install this as a submodule:

git submodule add git://github.com/coderxlsn/PhlyMongo vendor/PhlyMongo

Usage

Services

In order to remain as flexible as possible, the service factories require that you pass information to the constructors. As such, you should typically configure and setup the factories via your Module.php definition:

namespace My;

use PhlyMongo\MongoCollectionFactory;
use PhlyMongo\MongoDbFactory;

class Module
{
    public function getServiceConfig()
    {
        return array('factories' => array(
            'My\Mongo'           => 'PhlyMongo\MongoConnectionFactory',
            'My\MongoDB'         => new MongoDbFactory('my-site', 'My\Mongo'),
            'My\MongoCollection' => new MongoCollectionFactory('some-stuff', 'My\MongoDB'),
        ));
    }
}

If you want the server, server options, database, collection, or any service names to be dynamic, consider wrapping the factories in closures, and passing in configuration:

namespace My;

use PhlyMongo\MongoCollectionFactory;
use PhlyMongo\MongoConnectionFactory;
use PhlyMongo\MongoDbFactory;

class Module
{
    public function getServiceConfig()
    {
        return array('factories' => array(
            'My\Mongo'           => function ($services) {
                $config = $services->get('config');
                $config = $config['my']['mongo'];
                $factory = new MongoConnectionFactory($config['server'], $config['server_options']);
                return $factory->createService($services);
            },
            // and so on //
        ));
    }
}

However, if you need to do this, you might just as easily use the native Mongo classes.

Hydrating Cursor

The hydrating cursor is useful as a way to map result sets to objects.

Pass a MongoCursor instance to the constructor, along with a hydrator and a prototype object, and you're set:

use PhlyMongo\HydratingMongoCursor;
use Zend\Stdlib\Hydrator\ObjectProperty;

class Status
{
    public $_id;
    public $name;
    public $email;
    public $status;
}

$resultset = new HydratingMongoCursor(
    $collection->find(),
    new ObjectProperty,
    new Status
);
foreach ($resultset as $status) {
    printf('%s <%s>: %s', $status->name, $status->email, $status->status);
}

Paginator Adapter

The paginator adapter allows you to use a MongoCursor with Zend\Paginator.

Pass a MongoCursor to the constructor, and then pass the adapter to the paginator instance.

use PhlyMongo\PaginatorAdapter as MongoPaginatorAdapter;
use Zend\Paginator\Paginator;

$adapter   = new MongoPaginatorAdapter($collection->find());
$paginator = new Paginator($adapter);
$paginator->setCurrentPageNumber(5);
$paginator->setItemCountPerPage(10);

foreach ($paginator as $item) {
    // only receiving up to 10 items, starting at offset 50
}

Hydrating Paginator Adapter

This builds on the paginator adapter, and simply alters it to accept specifically a PhlyMongo\HydratingMongoCursor in the constructor, allowing you to return objects of a specific type during iteration.

use PhlyMongo\HydratingMongoCursor;
use PhlyMongo\HydratingPaginatorAdapter as MongoPaginatorAdapter;
use Zend\Paginator\Paginator;

$adapter   = new MongoPaginatorAdapter(new HydratingMongoCursor(
    $collection->find(),
    new ObjectProperty,
    new Status
));
$paginator = new Paginator($adapter);
$paginator->setCurrentPageNumber(5);
$paginator->setItemCountPerPage(10);

foreach ($paginator as $item) {
    // only receiving up to 10 items, starting at offset 50
    printf('%s <%s>: %s', $status->name, $status->email, $status->status);
}

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 15
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固