How to obtain cursor using external table files _ PHP Tutorial

Source: Internet
Author: User
Tags mongodb server testlink
Analyze the implementation method of getting cursor (cursor) by using an external table file. The MongoCursorObject cursor class uses the following code to copy the Config. php configuration file Table. php (mongodb operation database file) Config. php configuration file :? Phprequire_onceZendE MongoCursor Object
Cursor type

Mongo
Config. php configuration file
Table. php (mongodb operations on database files)

Config. php configuration file

The code is as follows:


Require_once 'zend/Exception. php ';
Class Hrs_Mongo_Config
{
Const VERSION = '1. 7.0 ';
Const DEFAULT_HOST = 'localhost ';
Const DEFAULT_PORT = 27017;
Private static $ host = self: DEFAULT_HOST;
Private static $ port = self: DEFAULT_PORT;
Private static $ options = array (
'Connect '=> true,
'Timeout' => 30,
// 'Replicaset '=> ''// If this is given, the master will be determined by using the ismaster database command on the seeds
);
Public static $ conn = '';
Public static $ defaultDb = '';
Public static $ linkStatus = '';
Public static function set ($ server = 'mongodb: // localhost: 8080', $ options = array ('connect '=> true )){
If (! $ Server ){
$ Url = 'mongodb: // '. self: $ host.': '. self: $ port;
}
If (is_array ($ server )){
If (isset ($ server ['host']) {
Self: $ host = $ server ['host'];
}
If (isset ($ server ['port']) {
Self: $ port = $ server ['port'];
}
If (isset ($ server ['user']) & isset ($ server ['pass']) {
$ Url = 'mongodb ://'. $ server ['user']. ':'. $ server ['pass']. '@'. self: $ host. ':'. self: $ port;
} Else {
$ Url = 'mongodb: // '. self: $ host.': '. self: $ port;
}
}
If (is_array ($ options )){
Foreach (self ::$ options as $ o_k => $ o_v ){
If (isset ($ options [$ o_k])
Self: $ options [$ o_k] = $ o_v;
}
}
Try {
Self: $ conn = new Mongo ($ url, self: $ options );
Self: $ linkStatus = 'success ';
} Catch (Exception $ e ){
Self: $ linkStatus = 'failed ';
}
If (isset ($ server ['database']) {
Self: selectDB ($ server ['database']);
}
}
Public static function selectDB ($ database ){
If ($ database ){
Try {
If (self: $ linkStatus = 'success ')
Self: $ defaultDb = self: $ conn-> selectDB ($ database );
Return self: $ defaultDb;
}
Catch (InvalidArgumentException $ e ){
Throw new Zend_Exception ('incorrect Mongodb database name ');
}
} Else {
Throw new Zend_Exception ('mongodb database name cannot be blank ');
}
}
}


Table. php (mongodb operations on database files)

The code is as follows:


Require_once 'hrs/Mongo/Config. php ';
Abstract class Hrs_Mongo_Table
{
Protected $ _ db = '';
Protected $ _ name = '';
Protected $ _ data = array ();
Protected $ c_options = array (
'Fsync' => true,
'Safe '=> true
);
Protected $ u_options = array (
// 'Upsert '=> false,
'Multiple '=> true,
'Fsync' => true,
'Safe '=> true
);
/*
Protected $ r_options = array (
);*/
Protected $ d_options = array (
'Fsync' => true,
'Justone '=> false,
'Safe '=> true
);
Protected function _ setAdapter ($ database = ''){
If (! $ Database)
Throw new Zend_Exception ('mongodb database name cannot be blank ');
Hrs_Mongo_Config: selectDB ($ database );
}
Public function _ construct (){
If (Hrs_Mongo_Config: $ conn instanceof Mongo ){
$ Name = $ this-> _ name;
$ DefDb = Hrs_Mongo_Config: $ defaultDb;
$ This-> _ db = $ defDb-> $ name;
} Else {
Throw new Zend_Exception ('mongodb server connection failed ');
}
}
Public function insert ($ data ){
If (! $ This-> testLink () return false;
$ Ret = $ this-> _ db-> insert ($ data, $ this-> c_options );
Return $ ret;
}
Public function update ($ data, $ where ){
If (! $ This-> testLink () return false;
Return $ this-> _ db-> update ($ where, $ data, $ this-> u_options );
}
Public function find ($ where = array (), $ limit = 0 ){
If ($ this-> testLink ()){
If ($ limit> 0 ){
$ This-> _ data = $ where? $ This-> _ db-> find ($ where)-> limit ($ limit)-> snapshot (): $ this-> _ db-> find () -> limit ($ limit)-> snapshot ();
} Else {
$ This-> _ data = $ where? $ This-> _ db-> find ($ where)-> limit ($ limit)-> snapshot (): $ this-> _ db-> find () -> limit ($ limit)-> snapshot ();
}
}
Return $ this;
}
// Find cursor
/*
* Get the cursor object
*/
Public function look ($ where = array (), $ fields = array ()){
If ($ this-> testLink ()){
If ($ fields ){
Return $ where? $ This-> _ db-> find ($ where, $ fields): $ this-> _ db-> find ()-> fields ($ fields );
} Else {
Return $ where? $ This-> _ db-> find ($ where): $ this-> _ db-> find ();
}
}
Return false;
}
Public function delete ($ where ){
If (! $ This-> testLink () return false;
Return $ this-> _ db-> remove ($ where, $ this-> d_options );
}
Public function dropMe (){
If (! $ This-> testLink () return false;
Return $ this-> _ db-> drop ();
}
Public function _ toString (){
Return $ this-> _ data;
}
Public function toArray (){
$ TmpData = array ();
Foreach ($ this-> _ data as $ id => $ row ){
$ One_row = array ();
Foreach ($ row as $ key => $ col ){
$ One_row [$ key] = $ col;
}
$ One_row ['_ id'] = $ id;
$ TmpData [] = $ one_row;
}
Return $ tmpData;
}
Protected function testLink (){
Return Hrs_Mongo_Config: $ linkStatus = 'success '? True: false;
}
}


Important notes !!!
Method 1

The code is as follows:


// Find cursor
/*
* Get the cursor object
*/
Public function look ($ where = array (), $ fields = array ()){
If ($ this-> testLink ()){
If ($ fields ){
Return $ where? $ This-> _ db-> find ($ where, $ fields): $ this-> _ db-> find ()-> fields ($ fields );
} Else {
Return $ where? $ This-> _ db-> find ($ where): $ this-> _ db-> find ();
}
}
Return false;
}


Method 2

The code is as follows:


Public function find ($ where = array (), $ field = array ()){
If ($ this-> testLink ()){
$ This-> _ data = $ this-> _ db-> find ($ where, $ field)-> sort (array ("_ id" =>-1 ));
}
Return $ this;
}


The code is as follows:


/*
* Get the cursor object
*/
Public function getCursor (){
Return $ this-> _ data;
}


The second requirement is that find is not an array.
Find ($ where)-> getCursor (); is a cursor Object

Note:
Find () returns the current object
The toArray () method is to convert the current object to an array.
The getCursor () method is to convert the current Object to a cursor Object (cursor Object)

Http://www.bkjia.com/PHPjc/327974.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327974.htmlTechArticleMongoCursor Object cursor class Mongo Config. php configuration file Table. php (mongodb operating database file) Config. php configuration file code is as follows :? Php require_once 'zend/E...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.