Skip to content

olamedia/kanon-mongo

Repository files navigation

kanon-mongo

PHP MongoDB ORM

Features

  • Separated models and connection setup (no collection names in models)
  • References
  • Default values
  • Retrieving native mongoclient objects (just in case)
  • Lazy-loading of references
  • Lazy-loading of models in result (not fetching all at once, not caching)
  • Memory-safe (see lazy-loading)

Class map

$autoload = array(
'ru\\olamedia\\kanon\\mongo\\connection' => 'connection.php',
'ru\\olamedia\\kanon\\mongo\\collection' => 'collection.php',
'ru\\olamedia\\kanon\\mongo\\model' => 'model.php',
'ru\\olamedia\\kanon\\mongo\\result' => 'result.php',
'ru\\olamedia\\kanon\\mongo\\reference' => 'reference.php',
'ru\\olamedia\\kanon\\mongo\\referenceSet' => 'referenceSet.php',
'ru\\olamedia\\kanon\\mongo\\typeManager' => 'typeManager.php',
);

Property types

  • string
  • int | integer
  • float | double (both are double in MongoDB)
  • bool | boolean
  • timestamp (\MongoTimestamp, MongoTimestamp is used by sharding. If you're not looking to write sharding tools, what you probably want is MongoDate.)
  • datetime | date | time (\MongoDate, MongoDB stores dates as milliseconds past the epoch.)
  • reference (@see http://php.net/mongodbref )
  • references (array of "reference"s)

Model definition

<?php
namespace my\namespace;

use ru\olamedia\kanon\mongo\model;

class article extends model{
  protected static $_properties = [
	    'parent' => [
	      'model'=>'my\\namespace\\article',
	      'type'=>'reference'
	    ],
		'name' => [
	      'default'=>'',
	      'type'=>'string'
	    ]
	];
}

Setting up connection

use ru\olamedia\kanon\mongo;

mongo\connection::getInstance('default')
  ->connect([
  	'connection' => [
			'hostnames' => 'localhost',
			'database'  => 'dbname',
		]
	])
  ->registerModel(
    'my\\namespace\\article', // class name
    'articles' // collection name
  );

Using models

$article1 = new article();
$article1->name = 'New article';
$article1->save();
$article2 = new article(['name' => 'Hello world']);
$article2->parent = $article1; // reference
$article2->save();
var_dump($article1->getId());
echo $article1->getId(); // MongoDB uses _id field with \MongoId objects convertable toString().
var_dump($article1->name);
$article1->delete();

Queries

$result = article::find($criteria, $fields);
$result = article::findOne($criteria, $fields);
// $result is instance of ru\olamedia\kanon\mongo\result,
// which is a wrapper to \MongoCursor able to return model instances instead of arrays

// to get \MongoCursor back:
$cursor = $result->getCursor();
// creating result from \MongoCursor:
$result = new result('my\\namespace\\article', $cursor);

// using native methods:
$result->asc('name');
$result->desc('name');
$result->sort(['name'=>1]); // @see http://php.net/mongocursor.sort
$result->skip(3); // @see http://php.net/mongocursor.skip
$result->limit(10); // see http://php.net/mongocursor.limit

// using collections:
$collection = collection::forClass('my\\namespace\\article'); // \ru\olamedia\kanon\mongo\collection
$mongoCollection = $collection->select(); // \MongoCollection
$cursor = $mongoCollection->find($criteria, $fields); // \MongoCursor
$result = new result('my\\namespace\\article', $cursor); // \ru\olamedia\kanon\mongo\result
foreach ($result as $article){ // \my\namespace\article
  echo $article->name.'<br />';
}

Result

Implements \Iterator, \Countable

$articles = article::find();
echo count($articles);
foreach ($articles as $article){
}

Requirements

  • PHP >= 5.3
  • PECL mongoclient >= 1.3.0

License

MIT

About

PHP MongoDB ORM

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages