DboSource::fetchAll PHP Method

fetchAll() public method

Returns false if no rows matched. ### Options - cache - Returns the cached version of the query, if exists and stores the result in cache. This is a non-persistent cache, and only lasts for a single request. This option defaults to true. If you are directly calling this method, you can disable caching by setting $options to false
public fetchAll ( string $sql, array | boolean $params = [], array $options = [] ) : boolean | array
$sql string SQL statement
$params array | boolean Either parameters to be bound as values for the SQL statement, or a boolean to control query caching.
$options array additional options for the query.
return boolean | array Array of resultset rows, or false if no rows matched
    public function fetchAll($sql, $params = array(), $options = array())
    {
        if (is_string($options)) {
            $options = array('modelName' => $options);
        }
        if (is_bool($params)) {
            $options['cache'] = $params;
            $params = array();
        }
        $options += array('cache' => true);
        $cache = $options['cache'];
        if ($cache && ($cached = $this->getQueryCache($sql, $params)) !== false) {
            return $cached;
        }
        $result = $this->execute($sql, array(), $params);
        if ($result) {
            $out = array();
            if ($this->hasResult()) {
                $first = $this->fetchRow();
                if ($first) {
                    $out[] = $first;
                }
                while ($item = $this->fetchResult()) {
                    if (isset($item[0])) {
                        $this->fetchVirtualField($item);
                    }
                    $out[] = $item;
                }
            }
            if (!is_bool($result) && $cache) {
                $this->_writeQueryCache($sql, $out, $params);
            }
            if (empty($out) && is_bool($this->_result)) {
                return $this->_result;
            }
            return $out;
        }
        return false;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * testRealQueries method
  *
  * @return void
  */
 public function testRealQueries()
 {
     $this->loadFixtures('Apple', 'Article', 'User', 'Comment', 'Tag', 'Sample', 'ArticlesTag');
     $Apple = ClassRegistry::init('Apple');
     $Article = ClassRegistry::init('Article');
     $result = $this->Dbo->rawQuery('SELECT color, name FROM ' . $this->Dbo->fullTableName('apples'));
     $this->assertTrue(!empty($result));
     $result = $this->Dbo->fetchRow($result);
     $expected = array($this->Dbo->fullTableName('apples', false, false) => array('color' => 'Red 1', 'name' => 'Red Apple 1'));
     $this->assertEquals($expected, $result);
     $result = $this->Dbo->fetchAll('SELECT name FROM ' . $this->Dbo->fullTableName('apples') . ' ORDER BY id');
     $expected = array(array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'Red Apple 1')), array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'Bright Red Apple')), array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'green blue')), array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'Test Name')), array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'Blue Green')), array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'My new apple')), array($this->Dbo->fullTableName('apples', false, false) => array('name' => 'Some odd color')));
     $this->assertEquals($expected, $result);
     $result = $this->Dbo->field($this->Dbo->fullTableName('apples', false, false), 'SELECT color, name FROM ' . $this->Dbo->fullTableName('apples') . ' ORDER BY id');
     $expected = array('color' => 'Red 1', 'name' => 'Red Apple 1');
     $this->assertEquals($expected, $result);
     $Apple->unbindModel(array(), false);
     $result = $this->Dbo->read($Apple, array('fields' => array($Apple->escapeField('name')), 'conditions' => null, 'recursive' => -1));
     $expected = array(array('Apple' => array('name' => 'Red Apple 1')), array('Apple' => array('name' => 'Bright Red Apple')), array('Apple' => array('name' => 'green blue')), array('Apple' => array('name' => 'Test Name')), array('Apple' => array('name' => 'Blue Green')), array('Apple' => array('name' => 'My new apple')), array('Apple' => array('name' => 'Some odd color')));
     $this->assertEquals($expected, $result);
     $result = $this->Dbo->read($Article, array('fields' => array('id', 'user_id', 'title'), 'conditions' => null, 'recursive' => 1));
     $this->assertTrue(Set::matches('/Article[id=1]', $result));
     $this->assertTrue(Set::matches('/Comment[id=1]', $result));
     $this->assertTrue(Set::matches('/Comment[id=2]', $result));
     $this->assertFalse(Set::matches('/Comment[id=10]', $result));
 }
All Usage Examples Of DboSource::fetchAll
DboSource