【问题标题】:phalcon 0.6.0 ODM (object-document mapper) for MongoDB how i can use?用于 MongoDB 的 phalcon 0.6.0 ODM(对象文档映射器)我该如何使用?
【发布时间】:2012-10-06 14:40:12
【问题描述】:

在公告中

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

谁能提供更多信息?

【问题讨论】:

    标签: mongodb phalcon


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2011-08-24
      • 2016-05-28
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多