Permalink
Browse files

BackwardsCompatibilityBreak - The fourth parameter of fRecordSet::bui…

…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 Sep 29, 2008
1 parent 5c3b153 commit 1c247eaa5588ebad277c323bc79afaaec6926b56
Showing with 17 additions and 24 deletions.
  1. +17 −24 classes/fRecordSet.php
View
@@ -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);
@@ -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);
}
}
@@ -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);
@@ -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;

0 comments on commit 1c247ea

Please sign in to comment.