{"id":13673848,"url":"https://github.com/phly/PhlyMongo","last_synced_at":"2025-04-28T13:31:53.133Z","repository":{"id":5071172,"uuid":"6233113","full_name":"phly/PhlyMongo","owner":"phly","description":"ZF2 module for handling Mongo services, resultsets, and pagination","archived":false,"fork":false,"pushed_at":"2015-10-20T20:38:31.000Z","size":358,"stargazers_count":27,"open_issues_count":0,"forks_count":17,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-01T18:11:58.461Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phly.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-10-15T19:43:56.000Z","updated_at":"2024-03-05T12:49:48.000Z","dependencies_parsed_at":"2022-08-19T06:31:15.393Z","dependency_job_id":null,"html_url":"https://github.com/phly/PhlyMongo","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phly%2FPhlyMongo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phly%2FPhlyMongo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phly%2FPhlyMongo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phly%2FPhlyMongo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phly","download_url":"https://codeload.github.com/phly/PhlyMongo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251319757,"owners_count":21570451,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T11:00:25.853Z","updated_at":"2025-04-28T13:31:48.083Z","avatar_url":"https://github.com/phly.png","language":"PHP","funding_links":[],"categories":["Table of Contents","Modules"],"sub_categories":["Zend Framework 2"],"readme":"PhlyMongo - ZF2 Module for Mongo Integration\n============================================\n\n[![Build Status](https://secure.travis-ci.org/phly/PhlyMongo.png?branch=master)](http://travis-ci.org/phly/PhlyMongo)\n\nPhlyMongo provides the following to assist with Mongo usage in ZF2:\n\n- Hydrating Mongo resultset\n- Mongo paginator adapter\n- Mongo paginator adapter for hydrating resultsets\n- Configurable service factories for the Mongo, MongoDB, and MongoCollection classes\n\nInstallation\n------------\n\nSimplest is to add the following to `composer.json`:\n\n```javascript\n{\n    \"minimum-stability\": \"dev\",\n    \"require\": {\n        \"phly/phly-mongo\": \"~1.0-dev\"\n    }\n}\n```\n\nAnd then run:\n\n```bash\nphp composer.phar install\n```\n\nAlternately, use git to install this as a submodule:\n\n```bash\ngit submodule add git://github.com/phly/PhlyMongo vendor/PhlyMongo\n```\n\nUsage\n-----\n\n### Services\n\nIn order to remain as flexible as possible, the service factories require that\nyou pass information to the constructors. As such, you should typically \nconfigure and setup the factories via your `Module.php` definition:\n\n```php\nnamespace My;\n\nuse PhlyMongo\\MongoCollectionFactory;\nuse PhlyMongo\\MongoDbFactory;\n\nclass Module\n{\n    public function getServiceConfig()\n    {\n        return array('factories' =\u003e array(\n            'My\\Mongo'           =\u003e 'PhlyMongo\\MongoConnectionFactory',\n            'My\\MongoDB'         =\u003e new MongoDbFactory('my-site', 'My\\Mongo'),\n            'My\\MongoCollection' =\u003e new MongoCollectionFactory('some-stuff', 'My\\MongoDB'),\n        ));\n    }\n}\n```\n\nIf you want the server, server options, database, collection, or any service\nnames to be dynamic, consider wrapping the factories in closures, and passing\nin configuration:\n\n```php\nnamespace My;\n\nuse PhlyMongo\\MongoCollectionFactory;\nuse PhlyMongo\\MongoConnectionFactory;\nuse PhlyMongo\\MongoDbFactory;\n\nclass Module\n{\n    public function getServiceConfig()\n    {\n        return array('factories' =\u003e array(\n            'My\\Mongo'           =\u003e function ($services) {\n                $config = $services-\u003eget('config');\n                $config = $config['my']['mongo'];\n                $factory = new MongoConnectionFactory($config['server'], $config['server_options']);\n                return $factory-\u003ecreateService($services);\n            },\n            // and so on //\n        ));\n    }\n}\n```\n\nHowever, if you need to do this, you might just as easily use the native Mongo\nclasses.\n\n### Hydrating Cursor\n\nThe hydrating cursor is useful as a way to map result sets to objects.\n\nPass a `MongoCursor` instance to the constructor, along with a hydrator and a\nprototype object, and you're set:\n\n```php\nuse PhlyMongo\\HydratingMongoCursor;\nuse Zend\\Stdlib\\Hydrator\\ObjectProperty;\n\nclass Status\n{\n    public $_id;\n    public $name;\n    public $email;\n    public $status;\n}\n\n$resultset = new HydratingMongoCursor(\n    $collection-\u003efind(),\n    new ObjectProperty,\n    new Status\n);\nforeach ($resultset as $status) {\n    printf('%s \u003c%s\u003e: %s', $status-\u003ename, $status-\u003eemail, $status-\u003estatus);\n}\n```\n\n### Paginator Adapter\n\nThe paginator adapter allows you to use a `MongoCursor` with `Zend\\Paginator`.\n\nPass a `MongoCursor` to the constructor, and then pass the adapter to the\npaginator instance.\n\n```php\nuse PhlyMongo\\PaginatorAdapter as MongoPaginatorAdapter;\nuse Zend\\Paginator\\Paginator;\n\n$adapter   = new MongoPaginatorAdapter($collection-\u003efind());\n$paginator = new Paginator($adapter);\n$paginator-\u003esetCurrentPageNumber(5);\n$paginator-\u003esetItemCountPerPage(10);\n\nforeach ($paginator as $item) {\n    // only receiving up to 10 items, starting at offset 50\n}\n```\n\n### Hydrating Paginator Adapter\n\nThis builds on the paginator adapter, and simply alters it to accept\nspecifically a `PhlyMongo\\HydratingMongoCursor` in the constructor, allowing\nyou to return objects of a specific type during iteration.\n\n```php\nuse PhlyMongo\\HydratingMongoCursor;\nuse PhlyMongo\\HydratingPaginatorAdapter as MongoPaginatorAdapter;\nuse Zend\\Paginator\\Paginator;\n\n$adapter   = new MongoPaginatorAdapter(new HydratingMongoCursor(\n    $collection-\u003efind(),\n    new ObjectProperty,\n    new Status\n));\n$paginator = new Paginator($adapter);\n$paginator-\u003esetCurrentPageNumber(5);\n$paginator-\u003esetItemCountPerPage(10);\n\nforeach ($paginator as $item) {\n    // only receiving up to 10 items, starting at offset 50\n    printf('%s \u003c%s\u003e: %s', $status-\u003ename, $status-\u003eemail, $status-\u003estatus);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphly%2FPhlyMongo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphly%2FPhlyMongo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphly%2FPhlyMongo/lists"}