Ver Mensaje Individual
  #22 (permalink)  
Antiguo 06/02/2012, 21:17
Avatar de GatorV
GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Consulta objetos PHP

Pues algo así podría funcionar:
Código PHP:
Ver original
  1. <?php
  2. class Query implements Iterator
  3. {
  4.     private $_limit;
  5.     private $_offset;
  6.     private $_query;
  7.     private $_executed;
  8.     private $_sampleData = array(
  9.         'row1',
  10.         'row2',
  11.         'row3',
  12.         'row4',
  13.         'row5',
  14.         'row6',
  15.         'row7',
  16.         'row8',
  17.         'row9',
  18.         'row10'
  19.     );
  20.    
  21.     public $_position;
  22.    
  23.     public function __construct()
  24.     {
  25.         $this->_query = 'SELECT * FROM foo';
  26.         $this->_position = 0;
  27.         $this->_executed = false;
  28.     }
  29.    
  30.     public function limit($results)
  31.     {
  32.         $this->_query .= " LIMIT $results";
  33.        
  34.         return $this;
  35.     }
  36.    
  37.     public function offset($results)
  38.     {
  39.         $this->_query .= " OFFSET $results";
  40.        
  41.         return $this;
  42.     }
  43.    
  44.     public function __toString()
  45.     {
  46.         return $this->_query;
  47.     }
  48.    
  49.     public function __invoke()
  50.     {
  51.         $query = (string) $this->__toString();
  52.         echo "Executing $query";
  53.         $this->_position = 0;
  54.         $this->_executed = true;
  55.         return $this->_sampleData;
  56.     }
  57.    
  58.     public function isExecuted()
  59.     {
  60.         return $this->_executed;
  61.     }
  62.    
  63.     public function execQuery()
  64.     {
  65.         $query = $this->__toString();
  66.         echo "Executing $query";
  67.         $this->_position = 0;
  68.         $this->_executed = true;
  69.     }
  70.    
  71.     public function rewind()
  72.     {
  73.         if (!$this->isExecuted()) {
  74.             $this->execQuery();
  75.         }
  76.         $this->_position = 0;
  77.     }
  78.    
  79.     public function current()
  80.     {
  81.         return $this->_sampleData[$this->_position];
  82.     }
  83.    
  84.     public function key()
  85.     {
  86.         return $this->_position;
  87.     }
  88.    
  89.     public function next()
  90.     {
  91.         ++$this->_position;
  92.     }
  93.    
  94.     public function valid()
  95.     {
  96.         return isset($this->_sampleData[$this->_position]);
  97.     }
  98. }
  99.  
  100. $query = new Query();
  101.  
  102. $query1 = clone $query;
  103. $query2 = clone $query;
  104. $query3 = clone $query;
  105. foreach($query1->limit(10) as $data) {
  106.     var_dump($data);
  107. }
  108. foreach($query1->limit(10) as $data) {
  109.     var_dump($data);
  110. }
  111. foreach($query2->offset(10) as $data) {
  112.     var_dump($data);
  113. }
  114. foreach ($query3->limit(10)->offset(10) as $data) {
  115.     var_dump($data);
  116. }