Php Yii,MongoDB查询子文档以插入Mysql表

Php Yii,MongoDB查询子文档以插入Mysql表,php,mysql,mongodb,yii,Php,Mysql,Mongodb,Yii,我正在使用Yii框架、MySQL和MongoDB做一个项目。我需要从mongo数据库的集合中获取数据,并将记录插入mysql表中。mongodb的文档有子文档,我需要在mysql中为集合中的每个子文档插入一行。我的项目代码是: $query = array('year'=>'2014'); $records = Yii::app()->edmsMongoCollection('DocumentosDesgloseER')->find($query);

我正在使用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循环吗

感谢您循环“子文档”。。。说:


我尝试了您的建议,但在执行代码时出现错误(内部错误服务器500)。谢谢你的回复:)事实上,我已经解决了我的问题,对你的回复做了一些修改。再次感谢。我每人做了两件事:
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;
        }
}