2016-11-08 1 views
0

새로운 PHP mongodb 드라이버가 hasNext 메소드를 제거했습니다. 새로운 mongodb 드라이버 인 MongoDB Driver Cursor에서 MongoCursor :: hasNext의 대안은 무엇입니까?

http://php.net/manual/en/class.mongodb-driver-cursor.php

MongoDB\Driver\Cursor implements Traversable { 

/* Methods */ 
    final private __construct (void) 
    final public MongoDB\Driver\CursorId getId (void) 
    final public MongoDB\Driver\Server getServer (void) 
    final public bool isDead (void) 
    final public void setTypeMap (array $typemap) 
    final public array toArray (void) 
} 

우리는 최신 버전 3.2 MongoDB를 PHP는 드라이버 1.1 MongoDB를 업그레이드하려고합니다. 우리는 리팩터링 할 코드의 일부 위치에서 hasNext를 사용했습니다. 나는이 https://secure.php.net/manual/en/class.mongodb-driver-cursor.php#118824

class MongodbCursor 
{ 
    public static function hasNext(\MongoDB\Driver\Cursor $cursor) 
    { 
     $it = new \IteratorIterator($cursor); 
     $it->rewind(); 
     return $it->valid(); 
    } 
} 

예를 들어, 사용하여 시도

$cursor = some mongo query to get cursor 

if (!MongodbCursor::hasNext($cursor)){ 

    // since there is no data in above cursor, another query to get new cursor 
    $cursor = 

} 

foreach ($cursor as $item) { 

} 

그것은 오류 아래에 제공,

Cursors cannot yield multiple iterators 

답변

0

당신은 커서가 비어 있는지 확인하기 위해 IteratorIterator 방법을 사용할 수 있습니다. 예를 들어 :

$cursor = $collection->find(array('key'=> 'value')); 
$it = new IteratorIterator($cursor); 
$it->rewind(); 

if (!$it->current()){ 
    // Cursor is empty 
    $cursor = $collection->find(array('anotherKey'=> 'anotherValue')); 
    $it = new IteratorIterator($cursor); 
    $it->rewind(); 
} 
// Iterator all docs 
while ($doc = $it->current()) { 
    // Do something 
    $it->next(); 
} 

MongoDB PHP Library CRUD Tutorials

+0

$ cursor-> toArray()이 효율적입니다 참조? 모든 문서를 가져 와서 배열로 변환합니까? – vishal

+0

왜 downvote 모르겠어요. 그 대답은 오랫동안 전에 사용하지 않기 위해 편집되었습니다. –

관련 문제