Started adding support for Mongo in the Model class. Also removed fetchAll and fetchColumn methods, all data is returned as an associative array for consistency and to simplify the fetching logic.
This commit is contained in:
parent
fce848e9bb
commit
795594a391
3 changed files with 74 additions and 83 deletions
|
@ -101,33 +101,17 @@ class Database_Mongo extends Database_Common
|
|||
}
|
||||
|
||||
/**
|
||||
* Fetch a single row from the database
|
||||
* Fetch records from the database
|
||||
*/
|
||||
public function fetch($collection, $query, $fields = null, $return_type = null)
|
||||
public function fetch($collection, $query = array(), $fields = array())
|
||||
{
|
||||
$this->open();
|
||||
|
||||
// Pulls the results based on the type
|
||||
$results = false;
|
||||
if ($return_type == 'all')
|
||||
{
|
||||
$results = $this->connection->$collection->find($query, $fields);
|
||||
}
|
||||
else
|
||||
{
|
||||
$results = $this->connection->$collection->findOne($query, $fields);
|
||||
}
|
||||
$results = $this->connection->$collection->find($query, $fields);
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all rows as an array
|
||||
*/
|
||||
public function fetchAll($collection, $query, $fields = null)
|
||||
{
|
||||
return $this->fetch($collection, $query, $fields, 'all');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -196,14 +196,14 @@ class Database_PDO_Common extends Database_Common
|
|||
}
|
||||
|
||||
/**
|
||||
* Fetch a single row from the database
|
||||
* Fetch records from the database
|
||||
*
|
||||
* @param string $sql statement to be executed
|
||||
* @param array $input_parameters optional key/values to be bound
|
||||
* @param string $return_type optional type of return set
|
||||
* @return mixed based on return type
|
||||
*/
|
||||
public function fetch($sql = null, $input_parameters = null, $return_type = null)
|
||||
public function fetch($sql = null, $input_parameters = null)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
|
@ -213,49 +213,10 @@ class Database_PDO_Common extends Database_Common
|
|||
}
|
||||
|
||||
// Pulls the results based on the type
|
||||
$results = false;
|
||||
switch ($return_type)
|
||||
{
|
||||
case 'column':
|
||||
$results = $this->results->fetchColumn(0);
|
||||
break;
|
||||
case 'all':
|
||||
$results = $this->results->fetchAll(PDO::FETCH_ASSOC);
|
||||
break;
|
||||
default:
|
||||
$results = $this->results->fetch(PDO::FETCH_ASSOC);
|
||||
break;
|
||||
}
|
||||
$results = $this->results->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a single column from the database
|
||||
*
|
||||
* This method assumes you want the first column in your select. If you
|
||||
* need 2 or more columns you should simply use fetch().
|
||||
*
|
||||
* @param string $sql statement to be executed
|
||||
* @param array $input_parameters optional key/values to be bound
|
||||
* @return string
|
||||
*/
|
||||
public function fetchColumn($sql = null, $input_parameters = null)
|
||||
{
|
||||
return $this->fetch($sql, $input_parameters, 'column');
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all rows as an array
|
||||
*
|
||||
* @param string $sql statement to be executed
|
||||
* @param array $input_parameters optional key/values to be bound
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAll($sql = null, $input_parameters = null)
|
||||
{
|
||||
return $this->fetch($sql, $input_parameters, 'all');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -82,6 +82,18 @@ class Model extends Object
|
|||
*/
|
||||
protected $table = false; // FROM
|
||||
|
||||
/**
|
||||
* Collection Name
|
||||
*
|
||||
* For compatibility with the naming conventions used by MongoDB, the
|
||||
* collection name can be specified. If the collection name is set, it will
|
||||
* set the table name value to it and proceed as normal.
|
||||
*
|
||||
* @access protected
|
||||
* @var mixed
|
||||
*/
|
||||
protected $collection = false;
|
||||
|
||||
/**
|
||||
* Joins
|
||||
*
|
||||
|
@ -216,41 +228,76 @@ class Model extends Object
|
|||
$this->loadParameters($parameters);
|
||||
}
|
||||
|
||||
// Starts with a basic SELECT ... FROM
|
||||
$this->sql = array(
|
||||
'SELECT ' . (is_array($this->fields) ? implode(', ', $this->fields) : $this->fields),
|
||||
'FROM ' . $this->table,
|
||||
);
|
||||
|
||||
// Pulls based on parameters
|
||||
if (is_array($type_or_parameters))
|
||||
// Overwrites the table name with the available collection name
|
||||
if ($this->collection != false)
|
||||
{
|
||||
$this->generateQuery();
|
||||
$this->table = $this->collection;
|
||||
}
|
||||
// Pulls by ID
|
||||
elseif (is_int($type_or_parameters))
|
||||
{
|
||||
$this->sql[] = 'WHERE id = :id LIMIT 1;';
|
||||
|
||||
$this->input_parameters = array('id' => $parameters);
|
||||
// If we're using an RDBMS (not Mongo) proceed with using SQL to pull the data
|
||||
if ($this->db->getDriver() != 'mongo')
|
||||
{
|
||||
// Starts with a basic SELECT ... FROM
|
||||
$this->sql = array(
|
||||
'SELECT ' . (is_array($this->fields) ? implode(', ', $this->fields) : $this->fields),
|
||||
'FROM ' . $this->table,
|
||||
);
|
||||
|
||||
// Pulls based on parameters
|
||||
if (is_array($type_or_parameters))
|
||||
{
|
||||
$this->generateQuery();
|
||||
}
|
||||
// Pulls by ID
|
||||
elseif (is_int($type_or_parameters))
|
||||
{
|
||||
$this->sql[] = 'WHERE id = :id LIMIT 1;';
|
||||
|
||||
$this->input_parameters = array('id' => $parameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
switch ($type_or_parameters)
|
||||
{
|
||||
// Updates query to use COUNT syntax
|
||||
case 'count':
|
||||
$this->sql[0] = 'SELECT COUNT(*) AS count';
|
||||
$this->generateQuery();
|
||||
break;
|
||||
|
||||
// Adds the rest of the query
|
||||
case 'list':
|
||||
$this->generateQuery();
|
||||
break;
|
||||
|
||||
// Leaves the query as is
|
||||
case 'all':
|
||||
break;
|
||||
|
||||
// Throws an error
|
||||
default:
|
||||
throw new Exception('Unknown query type');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->records = $this->db->fetch(implode(' ', $this->sql), (count($this->input_parameters) == 0 ? null : $this->input_parameters));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('Sorry, Mongo support in the PICKLES Model is not quite ready yet');
|
||||
|
||||
/*
|
||||
switch ($type_or_parameters)
|
||||
{
|
||||
// Updates query to use COUNT syntax
|
||||
case 'count':
|
||||
$this->sql[0] = 'SELECT COUNT(*) AS count';
|
||||
$this->generateQuery();
|
||||
break;
|
||||
|
||||
// Adds the rest of the query
|
||||
case 'list':
|
||||
$this->generateQuery();
|
||||
break;
|
||||
|
||||
// Leaves the query as is
|
||||
case 'all':
|
||||
$this->db->fetch($this->table, $this->input_parameters);
|
||||
break;
|
||||
|
||||
// Throws an error
|
||||
|
@ -258,10 +305,9 @@ class Model extends Object
|
|||
throw new Exception('Unknown query type');
|
||||
break;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
$this->records = $this->db->fetchAll(implode(' ', $this->sql), (count($this->input_parameters) == 0 ? null : $this->input_parameters));
|
||||
|
||||
$list_type = ($type_or_parameters == 'list');
|
||||
|
||||
// Flattens the data into a list
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue