【问题标题】:Yii, MongoDB query Subdocuments for insertion in a Mysql TableYii,MongoDB查询子文档以插入Mysql表
【发布时间】:2014-05-06 15:26:11
【问题描述】:

我正在做一个使用 Yii 框架、MySQL 和 MongoDB 的项目。我需要从 mongo 数据库的集合中获取数据并将记录插入 mysql 表中。 mongodb 的文档有子文档,我需要在 mysql 中为集合中的每个子文档插入一行。我的项目代码是:

    $query = array('year'=>'2014');
    $records = Yii::app()->edmsMongoCollection('DocumentosDesgloseER')->find($query);       

    foreach($records as $data){
        $success = false;
        $model = new DesgloseAcumulado;
        $atributos['Id_Enterprise'] = $data['id_enterprise'];
        $atributos['Id_Store'] = $data['id_store'];
        $atributos['Month'] = $data['month'];
        $atributos['Year'] = $data['year'];
        $atributos['Id_Usuario'] = Yii::app()->user->id;
        $atributos['Id_RubroER'] = $data['DocumentosDesgloseER'][0]['id_rubroer'];
        $i = $i + 1;
        $model->attributes = $atributos;
        if($model->save()) {
            $success = true;
        }
    }

而我在mongodb中的集合结构是:

{
"_id" : ObjectId("535fe63acac5853943166f5c"),
"DocumentosDesgloseER" : [ 
    {
        "elemento" : "VENTAS NETAS",
        "id_rubroer" : "45",
        "id_documento_consolidados_temp" : "31809",
        "abreviatura" : "VN",
        "orden" : "1",
        "formula" : "(250*CM)",
        "tipo_fila" : "1",
        "resultado_actual" : "113.628",
        "resultado_ant" : "239.851",
        "promedio_mensual_actual" : "111.365",
        "ppto_mes_actual" : "131.540",
        "real_acumulado_actual" : "556.826",
        "real_acumulado_ant" : "965.824",
        "ppto_acumulado_actual" : "666.805"
    }, 
    {
        "elemento" : "RUBRO DE PRUEBAS",
        "id_rubroer" : "19",
        "id_documento_consolidados_temp" : "31810",
        "abreviatura" : "RP",
        "orden" : "3",
        "formula" : "CM/500",
        "tipo_fila" : "1",
        "resultado_actual" : "71.057",
        "resultado_ant" : "281.697",
        "promedio_mensual_actual" : "67.090",
        "ppto_mes_actual" : "72.347",
        "real_acumulado_actual" : "335.448",
        "real_acumulado_ant" : "879.719",
        "ppto_acumulado_actual" : "366.743"
    }
],
"documentoID" : "4DOTA2",
"id_enterprise" : "1",
"id_store" : "24",
"month" : "Importes2",
"year" : "2014",
"lastMonthNumber" : NumberLong(4)

}

这只是集合中的文档之一。正如我之前所说,我需要为mysql数据库中的每个文档的每个子文档保存一行。我的代码只保存了文档的数据和子文档“0”的id_rubroer。

有谁知道我如何为每个子文档而不是每个对象(文档)制作 For 循环?

谢谢

【问题讨论】:

    标签: php mysql mongodb yii


    【解决方案1】:

    只是循环“子文档”......说:

    foreach($records['DocumentosDesgloseER'] as $data){
            $success = false;
            $model = new DesgloseAcumulado;
            $atributos['Id_Enterprise'] = $data['id_enterprise'];
            $atributos['Id_Store'] = $data['id_store'];
            $atributos['Month'] = $data['month'];
            $atributos['Year'] = $data['year'];
            $atributos['Id_Usuario'] = Yii::app()->user->id;
            $atributos['Id_RubroER'] = $data['id_rubroer'];
            $i = $i + 1;
            $model->attributes = $atributos;
            if($model->save()) {
                $success = true;
            }
    }
    

    【讨论】:

    • 我尝试了您的建议,但在执行代码时出现错误(内部错误服务器 500)。无论如何感谢您的回复:)
    • 实际上,我已经解决了我的问题,对您的回复进行了一些更改。再次感谢。我做了两个 foreach:
    猜你喜欢
    • 2013-02-13
    • 2014-06-21
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 2018-09-04
    相关资源
    最近更新 更多