3

在公告中

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

有人可以提供更多信息吗?

4

1 回答 1

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

由于此功能正在全面开发中,某些方面可能会在最终版本之前发生变化。

于 2012-10-16T16:48:40.790 回答