2014-09-24 2 views
0

как я могу получить параметры без цикла, только читать массив $ find?Sokil MongoDB читать из базы данных

$collection = $tmp->getCollection('X'); 
$document = [ 
    'Me' => 'Keyonkeu', 
    'Maman' => 'Noubissie', 
    'Soeur' => 'Manuella', 
    'Papa' => 'SSadi', 
    'Age' => 5 
]; 
$collection->createDocument($document)->save(); 
$find = $collection->find()->where('Age', 5); 
var_dump($find); 

var_dump возвращает мне много классов с частными параметрами. Я не могу напрямую прочитать отдельный параметр (частный). Hier - это wath var_dump ($ find) return.

class Sokil\Mongo\QueryBuilder#29 (11) { 
    private $_client => 
    class Sokil\Mongo\Client#24 (7) { 
    private $_dsn => 
    string(19) "mongodb://127.0.0.1" 
    private $_connectOptions => 
    array(1) { 
     'connect' => 
     bool(true) 
    } 
    private $_connection => 
    class MongoClient#25 (4) { 
     public $connected => 
     bool(true) 
     public $status => 
     NULL 
     protected $server => 
     NULL 
     protected $persistent => 
     NULL 
    } 
    private $_databasePool => 
    array(1) { 
     'EmoBD' => 
     class Sokil\Mongo\Database#23 (6) { 
     ... 
     } 
    } 
    protected $_mapping => 
    array(0) { 
    } 
    private $_logger => 
    NULL 
    private $_currentDatabaseName => 
    string(5) "EmoBD" 
    } 
    private $_collection => 
    class Sokil\Mongo\Collection#27 (6) { 
    protected $_queryBuliderClass => 
    string(25) "\Sokil\Mongo\QueryBuilder" 
    protected $_queryExpressionClass => 
    string(23) "\Sokil\Mongo\Expression" 
    private $_database => 
    class Sokil\Mongo\Database#23 (6) { 
     private $_client => 
     class Sokil\Mongo\Client#24 (7) { 
     ... 
     } 
     private $_databaseName => 
     string(5) "EmoBD" 
     private $_mongoDB => 
     class MongoDB#26 (2) { 
     ... 
     } 
     private $_mapping => 
     array(0) { 
     ... 
     } 
     private $_classPrefix => 
     NULL 
     private $_collectionPool => 
     array(1) { 
     ... 
     } 
    } 
    private $_mongoCollection => 
    class MongoCollection#28 (2) { 
     public $w => 
     int(1) 
     public $wtimeout => 
     int(10000) 
    } 
    private $_documentsPool => 
    array(0) { 
    } 
    protected $_documentPoolEnabled => 
    bool(false) 
    } 
    private $_fields => 
    array(0) { 
    } 
    private $_cursor => 
    NULL 
    private $_skip => 
    int(0) 
    private $_expression => 
    class Sokil\Mongo\Expression#30 (1) { 
    protected $_expression => 
    array(1) { 
     'Age' => 
     int(5) 
    } 
    } 
    private $_limit => 
    int(0) 
    private $_sort => 
    array(0) { 
    } 
    private $_readPreferences => 
    array(0) { 
    } 
    protected $_queryExpressionClass => 
    NULL 
    private $_options => 
    array(2) { 
    'arrayResult' => 
    bool(false) 
    'expressionClass' => 
    string(23) "\Sokil\Mongo\Expression" 
    } 
} 

Как читать $ find-Array? что-то вроде:

$find['...']->get('...') 

я не хочу, чтобы использовать цикл

ответ

0

$ collection-> find() -> где ('Age', 5) возвращает курсор, а не документ или список документов. Поэтому вам нужно получить документ или список документов.

// get cursor which incapsulates all matched documents 
$cursor = $collection->find()->where('Age', 5); 
// get first from matched documents 
echo $cursor->findOne()->get('Age'); 
// get random from matched documents 
echo $cursor->findRandom()->get('Age'); 
// get all matched documents as array 
$documentList = $cursor->findAll(); 
echo $documentList['some-document-id']->get('Age'); 
+0

nice i работает нормально. Спасибо. – emoleumassi

+0

хороший я отлично работает. Спасибо. У меня есть другой вопрос: как я могу получить все элементы в виде массива: $ cursor = $ collection-> findAll(); и $ cursor - это массив – emoleumassi

+0

, спасибо, я пробовал ваше решение, но у меня есть структура класса с частными параметрами somethig вроде массива (3) { 'documentId' => класс Sokil \ Mongo \ Document # 36 (11) { private $ _resolvedRelations => массив (0) { } . невозможно получить непосредственно массив? – emoleumassi

0

QueryBuilder класс Sokil простирается Cursor, который, в свою очередь, имеет метод findAll() терминатора, который преобразует базовый объект MongoCursor в массив с iterator_to_array(). Это обсуждается в документации проекта по адресу querying documents.

Смежные вопросы