PHP将函数转换为MongoDB

时间:2016-08-11 09:50:38

标签: php node.js mongodb

我试图将我的一些PHP函数转换为mongodb,但是无法弄清楚,有人可以帮助我吗?

PHP函数应用程序:

php file.php

我对功能应用程序的尝试:

filebyte filename

PHP函数application_done

  function applications($gangId) {
        $applications = $this->db->query("SELECT * FROM `gang_applications` where `status`='avaliable' and `gangid`='$gangId'");
        return ($applications ? $applications : false);
    }

我对函数application_done的尝试

    gangshema.methods.applications(thisid) {
        // some sort of callback?
        db.gang_applications.find({status:avaliable, gangid: thisid}, function(err, cursor) {

            if (cursor != 0) {
                console.log(cursor);
            }

        });
    }

但我的主要概念是一个名为accept_applications的函数。我不清楚如何做这个部分,包括为响应调用其他函数。

 function application_done($applicationId) {
        $applications = $this->db->query("SELECT * FROM `gang_applications` where `id`='$applicationId'")->row();

        return ($applications->status == 'avaliable' ? false : true);
    }

1 个答案:

答案 0 :(得分:2)

在Node.js脚本的开头使用它:

var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/test';

第一个片段:

function applications($gangId) {
    $applications = $this->db->query("SELECT * FROM `gang_applications` where `status`='avaliable' and `gangid`='$gangId'");
    return ($applications ? $applications : false);
}

变为:

var findApplications = function(db, gangId) {
   var cursor = db.collection('gang_application').find({
     "status": "available" 
     "gangId": gangId
   });
   var results = [];
   cursor.each(function(err, doc) {
      if (err) console.log("Error: " + err);
      } else if (doc != null) {
         console.log("Null document.");
      } else {
         results.push(doc);
      }
   });
   return results;
};

第二个片段:

function application_done($applicationId) {
    $applications = $this->db->query("SELECT * FROM `gang_applications` where `id`='$applicationId'")->row();

    return ($applications->status == 'avaliable' ? false : true);
}

变为:

function applications(gangId) {
  db.gang_application
}
var applicationsDone = function(db, applicationId) {
   var cursor = db.collection('gang_application').find({
     "id": applicationId
   });
   var results = [];
   cursor.each(function(err, doc) {
      if (err) {
        console.log("Error: " + err);
      } else if (doc != null) {
         console.log("Null document.");
      } else {
         results.push(doc.status);
      }
   });
   return results;
};

调用如下:

MongoClient.connect(url, function(err, db) {
  if (!db) {
    console.log("Database did not connect.");
  }
  else {
    findApplications(db, "102"); // Replace "102" with gangId
    applicationsDone(db, "104"); // Replace "104" with applicationId
  }
});

编辑每条评论,以下是如何加入回调:

// Note extra `callback` parameter
var applicationsDone = function(db, applicationId, callback) {
   var cursor = db.collection('gang_application').findOne({
     "id": applicationId
   });
   cursor.each(function(err, doc) {
      if (err) {
        console.log("Error: " + err);
      } else if (doc != null) {
         console.log("Null document.");
      } else {
         return (doc.status == "available");
      }
   });
};

致电:

MongoClient.connect(url, function(err, db) {
  if (!db) {
    console.log("Database did not connect.");
  }
  else {
    var callback = function(doc) { console.log(doc.status); };
    applicationsDone(db, "104", callback);
  }
});

编辑第三段:

function accept_application($userid, $applicationId) {
        $box = 'failure';

        if (empty($applicationId)) {
            $message = "applicationId is empty";
        } elseif ($this->application_done($applicationId)) {
            $message = "Already registred!";

        } else {
            $application = $this->getApplication($applicationId);
            $test = true;
            if(!($test)) {
                $message = "false test";
            } else {
                $this->db->query("UPDATE `gang_applications` SET `status`='accepted', `by`='$userid' where `id`='$applicationId'");
                $this->gangs->add_member($application->userid,'gang','member',$application->gangid);
                $message = "Accept!";
            }

        }
        return $message;
    }

成为这个。请注意上述函数的一些更改,以使其工作,例如从applications返回文档数组,并使用.findOne() applicationDone()

function acceptApplication(userId, applicationId) {
  if (!applicationId) return "No application ID";
  if (applicationDone(db, applicationId)) return "Application already submitted.";

  // Left out `if (!$test)`
  db.gang_applications.update({
    "id": applicationId,
    "userId": userId
  }, {
    "status": "accepted"
  }, upsert: false);

  //                    $this->gangs->add_member($application->userid,'gang','member',$application->gangid);

  return "Accepted!";
}