Skip to content

Commit

Permalink
BackwardsCompatibilityBreak - The fourth parameter of fRecordSet::bui…
Browse files Browse the repository at this point in the history
…ld() is now the page number instead of the offset. The page number is subtracted by one and multiplied by the limit to get the correct offset.
  • Loading branch information
wbond committed Apr 12, 2012
1 parent 5c3b153 commit 1c247ea
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions classes/fRecordSet.php
Expand Up @@ -66,10 +66,10 @@ class fRecordSet implements Iterator
* @param array $where_conditions The column => value comparisons for the where clause
* @param array $order_bys The column => direction values to use for sorting
* @param integer $limit The number of records to fetch
* @param integer $offset The offset to use before limiting
* @param integer $page The page offset to use when limiting records
* @return fRecordSet A set of {@link fActiveRecord} objects
*/
static public function build($class, $where_conditions=array(), $order_bys=array(), $limit=NULL, $offset=NULL)
static public function build($class, $where_conditions=array(), $order_bys=array(), $limit=NULL, $page=NULL)
{
self::configure($class);

Expand Down Expand Up @@ -109,8 +109,19 @@ static public function build($class, $where_conditions=array(), $order_bys=array

$sql .= ' LIMIT ' . $limit;

if ($offset !== NULL) {
$sql .= ' OFFSET ' . $offset;
if ($page !== NULL) {

if (!is_numeric($page) || $page < 1) {
fCore::toss(
'fProgrammerException',
fGrammar::compose(
'The page specified, %s, is not a number or less than one',
fCore::dump($page)
)
);
}

$sql .= ' OFFSET ' . (($page-1) * $limit);
}
}

Expand All @@ -133,19 +144,14 @@ static public function buildFromRecords($class, $records)
self::configure($class);
$table = fORM::tablize($class);

$sql = 'SELECT ' . $table . '.* FROM ' . $table . ' WHERE ';

// Build the where clause
// Extract the primary key values
$primary_key_fields = fORMSchema::getInstance()->getKeys($table, 'primary');
$total_pk_fields = sizeof($primary_key_fields);
$total_pk_fields = sizeof($primary_key_fields);

$primary_keys = array();

$i = 0;
foreach ($records as $record) {
$sql .= ($i > 0) ? ' OR ' : '';
$sql .= ($total_pk_fields > 1) ? ' (' : '';

for ($j=0; $j < $total_pk_fields; $j++) {
$pk_field = $primary_key_fields[$j];
$pk_get_method = 'get' . fGrammar::camelize($pk_field, TRUE);
Expand All @@ -159,24 +165,11 @@ static public function buildFromRecords($class, $records)
if ($total_pk_fields > 1) {
$primary_keys[$i][$pk_field] = $pk_value;
}

$sql .= ($j > 0) ? ' AND ' : '';
$sql .= $table . '.' . $pk_field . fORMDatabase::escapeBySchema($table, $pk_field, $pk_value, '=');
}

$sql .= ($total_pk_fields > 1) ? ') ' : '';
$i++;
}

// Empty sets have SQL that won't return anything
if (sizeof($records) == 0) {
$sql .= " 0 = 1";
}

$result = new fResult(fORMDatabase::getInstance()->getType(), 'array');
$result->setResult(array());
$result->setReturnedRows(sizeof($records));
$result->setSQL($sql);

$record_set = new fRecordSet($class, $result);
$record_set->records = $records;
Expand Down

0 comments on commit 1c247ea

Please sign in to comment.