Question

in announcement

0.6.0 includes basic ODM (object-document mapper) for MongoDB

can anybody provide a little more information?

Was it helpful?

Solution

Phalcon 0.6.0 will provide a ODM to manipulate Mongo documents in an object oriented way. This isn't the definitive usage, but it'll give you an idea:

<?php

//Register the mongo db connection in the DI
$di->set('mongo', function() {
     $mongo = new Mongo("mongodb://localhost");
     return $mongo->selectDB('invo');
});

//Register a collection manager
$di->set('collectionManager', function() {
     return new Phalcon\Mvc\Collection\Manager();
});

//A model that maps to the products collection
class Products extends Phalcon\Mvc\Collection
{

}

//Create a document
$product = new Products();
$product->name = 'Artichoke';
$product->status = 'Active';
$product->save();

//Create another document
$product = new Products();
$product->name = 'Carrots';
$product->price = 15.20;
$product->status = 'Active';
$product->save();

//Updating a product
$product = Products::findFirst();
$product->status = 'Inactive';
$product->save();

//Deleting a product
$product = Products::findFirst();
$product->delete();

//Finding documents
$products = Products::find();
foreach($products as $product){
   echo $product->name;
}

$products = Products::find(array(
      'conditions' => array('$gt' => array('price', '5')),
      'sort' => array('name' => 1),
      'limit' => 2
));

Since this feature is in full development, some aspects may change until the final version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top