EMongoCursor.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * EMongoCursor.php
  4. *
  5. * PHP version 5.2+
  6. *
  7. * @author Nagy Attila Gábor <nagy.attila.gabor@gmail.com>
  8. * @author Dariusz Górecki <darek.krk@gmail.com>
  9. * @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
  10. * @copyright 2011 CleverIT http://www.cleverit.com.pl
  11. * @license http://www.yiiframework.com/license/ BSD license
  12. * @version 1.3
  13. * @category ext
  14. * @package ext.YiiMongoDbSuite
  15. *
  16. */
  17. /**
  18. * EMongoCursor
  19. *
  20. * Cursor object, that behaves much like the MongoCursor,
  21. * but this one returns instantiated objects
  22. * @since v1.3.4
  23. */
  24. class EMongoCursor
  25. implements Iterator, Countable
  26. {
  27. /**
  28. * @var MongoCursor $_cursor the MongoCursor returned by the query
  29. * @since v1.3.4
  30. */
  31. protected $_cursor;
  32. /**
  33. * @var EMongoDocument $_model the model used for instantiating objects
  34. * @since v1.3.4
  35. */
  36. protected $_model;
  37. /**
  38. * Construct a new EMongoCursor
  39. *
  40. * @param MongoCursor $cursor the cursor returned by the query
  41. * @param EMongoDocument $model the model for instantiating objects
  42. * @since v1.3.4
  43. */
  44. public function __construct(MongoCursor $cursor, EMongoDocument $model)
  45. {
  46. $this->_cursor = $cursor;
  47. $this->_model = $model;
  48. }
  49. /**
  50. * 返回当前的模型
  51. */
  52. public function getModel(){
  53. return $this->_model;
  54. }
  55. /**
  56. * Return MongoCursor for additional tuning
  57. *
  58. * @return MongoCursor the cursor used for this query
  59. * @since v1.3.4
  60. */
  61. public function getCursor()
  62. {
  63. return $this->_cursor;
  64. }
  65. /**
  66. * Return the current element
  67. * @return EMongoDocument
  68. * @since v1.3.4
  69. */
  70. public function current()
  71. {
  72. $document = $this->_cursor->current();
  73. if(empty($document))
  74. return $document;
  75. return $this->_model->populateRecord($document);
  76. }
  77. /**
  78. * Return the key of the current element
  79. * @return scalar
  80. * @since v1.3.4
  81. */
  82. public function key()
  83. {
  84. return $this->_cursor->key();
  85. }
  86. /**
  87. * Move forward to next element
  88. * @return void
  89. * @since v1.3.4
  90. */
  91. public function next()
  92. {
  93. $this->_cursor->next();
  94. }
  95. /**
  96. * Rewind the Iterator to the first element
  97. * @return void
  98. * @since v1.3.4
  99. */
  100. public function rewind()
  101. {
  102. $this->_cursor->rewind();
  103. }
  104. /**
  105. * Checks if current position is valid
  106. * @return boolean
  107. * @since v1.3.4
  108. */
  109. public function valid()
  110. {
  111. return $this->_cursor->valid();
  112. }
  113. /**
  114. * Returns the number of documents found
  115. * {@see http://www.php.net/manual/en/mongocursor.count.php}
  116. * @param boolean $foundOnly default FALSE
  117. * @return integer count of documents found
  118. * @since v1.3.4
  119. */
  120. public function count($foundOnly = false)
  121. {
  122. return $this->_cursor->count($foundOnly);
  123. }
  124. /**
  125. * Apply a limit to this cursor
  126. * {@see http://www.php.net/manual/en/mongocursor.limit.php}
  127. * @param integer $limit new limit
  128. * @since v1.3.4
  129. */
  130. public function limit($limit)
  131. {
  132. $this->_cursor->limit($limit);
  133. }
  134. /**
  135. * Skip a $offset records
  136. * {@see http://www.php.net/manual/en/mongocursor.skip.php}
  137. * @param integer $skip new skip
  138. * @since v1.3.4
  139. */
  140. public function offset($offset)
  141. {
  142. $this->_cursor->offset($offset);
  143. }
  144. /**
  145. * Apply sorting directives
  146. * {@see http://www.php.net/manual/en/mongocursor.sort.php}
  147. * @param array $sort sorting directives
  148. * @since v1.3.4
  149. */
  150. public function sort(array $fields)
  151. {
  152. $this->_cursor->sort($fields);
  153. }
  154. }