exs/simple-mongo-bundle

用于在PHP7中持久化并检索MongoDB信息的包

v2.0.0 2019-08-12 10:26 UTC

This package is auto-updated.

Last update: 2024-08-28 01:48:16 UTC


README

一个简单的包,用于在PHP7中对MongoDB数据库进行持久化和查询

在新的Symfony2项目中安装 # EXS-SimpleMongoBundle

编辑 composer.json 文件并添加 # EXS-SimpleMongoBundle 依赖

//composer.json
//...
"require": {
    //other bundles
    "exs/simple-mongo-bundle": "^1.0"
},

保存文件,并通过命令行运行 composer update 更新项目

composer update exs/simple-mongo-bundle

更新 app/AppKernel.php

//app/AppKernel.php
//...
public function registerBundles()
{
    $bundles = array(
    //Other bundles
    new EXS\SimpleMongoBundle\EXSSimpleMongoBundle(),
);

用法

在参数文件中添加您的mongodb连接和dbname

    simple_mongo:
        connection: mongodb://:27017
        dbname: YOUR_DB_NAME

创建新的集合(可选)

app/console exs:create:collection COLLECTION_NAME(Requires) OPTIONS
// Options
app/console exs:create:collection -h

在您的控制器或服务中

// Insert data to mongodb
$entity = new YourEntity();
$entity->setPropertyValue(THE_VALUE);
.
.

$manager = $this->get('exs_simple_mongo.service'); // get the service
$manager->persist($entity);   
$result = $manager->flush(COLLECTION_NAME); // the result will store the number of inserted entries or error message
if(!is_int($result) || $result == 0) {
    throwException($result);
}

// Update
$filter = ['product' => 6];
$manager->update($filter, $entity);   
$result = $manager->flush(COLLECTION_NAME); 
 
// Get data with query
$filter = ['product' => 6];
$option = ['projection' => ['_id' => 0]];

$manager = $this->get('exs_simple_mongo.service'); // get the service
$result = $manager->exeQuery($filter, $option, COLLECTION_NAME);
// $result will contain results in an array

贡献

任何人都可以贡献。

如果您有任何问题或建议,请 告诉我们