DboSource::query PHP Method

query() public method

DataSource Query abstraction
public query ( ) : resource
return resource Result resource identifier.
    public function query()
    {
        $args = func_get_args();
        $fields = null;
        $order = null;
        $limit = null;
        $page = null;
        $recursive = null;
        if (count($args) === 1) {
            return $this->fetchAll($args[0]);
        } elseif (count($args) > 1 && (strpos($args[0], 'findBy') === 0 || strpos($args[0], 'findAllBy') === 0)) {
            $params = $args[1];
            if (substr($args[0], 0, 6) === 'findBy') {
                $all = false;
                $field = Inflector::underscore(substr($args[0], 6));
            } else {
                $all = true;
                $field = Inflector::underscore(substr($args[0], 9));
            }
            $or = strpos($field, '_or_') !== false;
            if ($or) {
                $field = explode('_or_', $field);
            } else {
                $field = explode('_and_', $field);
            }
            $off = count($field) - 1;
            if (isset($params[1 + $off])) {
                $fields = $params[1 + $off];
            }
            if (isset($params[2 + $off])) {
                $order = $params[2 + $off];
            }
            if (!array_key_exists(0, $params)) {
                return false;
            }
            $c = 0;
            $conditions = array();
            foreach ($field as $f) {
                $conditions[$args[2]->alias . '.' . $f] = $params[$c++];
            }
            if ($or) {
                $conditions = array('OR' => $conditions);
            }
            if ($all) {
                if (isset($params[3 + $off])) {
                    $limit = $params[3 + $off];
                }
                if (isset($params[4 + $off])) {
                    $page = $params[4 + $off];
                }
                if (isset($params[5 + $off])) {
                    $recursive = $params[5 + $off];
                }
                return $args[2]->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
            }
            if (isset($params[3 + $off])) {
                $recursive = $params[3 + $off];
            }
            return $args[2]->find('first', compact('conditions', 'fields', 'order', 'recursive'));
        }
        if (isset($args[1]) && $args[1] === true) {
            return $this->fetchAll($args[0], true);
        } elseif (isset($args[1]) && !is_array($args[1])) {
            return $this->fetchAll($args[0], false);
        } elseif (isset($args[1]) && is_array($args[1])) {
            if (isset($args[2])) {
                $cache = $args[2];
            } else {
                $cache = true;
            }
            return $this->fetchAll($args[0], $args[1], array('cache' => $cache));
        }
    }

Usage Example

Exemplo n.º 1
0
/**
 * test that describe does not corrupt UUID primary keys
 *
 * @return void
 */
	public function testDescribeWithUuidPrimaryKey() {
		$tableName = 'uuid_tests';
		$this->Dbo->query("CREATE TABLE {$tableName} (id VARCHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
		$Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
		$result = $this->Dbo->describe($Model);
		$expected = array(
			'type' => 'string',
			'length' => 36,
			'null' => false,
			'default' => null,
			'key' => 'primary',
		);
		$this->assertEqual($result['id'], $expected);
		$this->Dbo->query('DROP TABLE ' . $tableName);

		$tableName = 'uuid_tests';
		$this->Dbo->query("CREATE TABLE {$tableName} (id CHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
		$Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
		$result = $this->Dbo->describe($Model);
		$expected = array(
			'type' => 'string',
			'length' => 36,
			'null' => false,
			'default' => null,
			'key' => 'primary',
		);
		$this->assertEqual($result['id'], $expected);
		$this->Dbo->query('DROP TABLE ' . $tableName);
	}
All Usage Examples Of DboSource::query
DboSource