用于MongoDB的phalcon 0.6.0 ODM(对象文件映射器)如何使用?

时间:2012-10-16 13:23:52

标签: mongodb phalcon

在公告中

  

0.6.0包含MongoDB的基本ODM(对象文档映射器)

任何人都可以提供更多信息吗?

1 个答案:

答案 0 :(得分:3)

Phalcon 0.6.0将提供ODM以面向对象的方式操作Mongo文档。这不是明确的用法,但它会给你一个想法:

<?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
));

由于此功能正在全面开发中,因此某些方面可能会更改,直到最终版本。