klenwell information services : Paste20090117

Revision history for Paste20090117


Revision [1261]

Last edited on 2010-04-12 07:09:51 by KlenwellAdmin
Additions:
/*
What happens here: model retrieves all records, then loops through them
to filter out the desired records based on the status values passed in
the extra array argument. Not the most efficient method, perhaps, but
certainly simpler than and safer than crafting a custom SQL query.

Note: the argument list is fixed by the CakePhp framework. Use the extra
argument if you need pass additional values.
*/
$Result = array();
$PreResult = $this->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));

foreach ( $PreResult as $Rec )
{
if ( in_array($Rec['ExtagMod'][0]['status'], $extra['statuses']) )
{
$Result[] = $Rec;
}
}
#pr(array('paginate result' => $Result));
return $Result;
$StatusList = ( isset($extra['statuses']) ) ? $extra['statuses'] : array(1);
if ( !is_array($StatusList) ) $StatusList = array( $StatusList );
$statuses = implode(',', $StatusList);

$sql = <<<MYSQL
$count = count($this->query($sql));
#pr(array('paginateCount result' => $count));
return $count;
$model_name => array
(
'recursive' => 2,
'limit' => 10,
'order' => array("$model_name.created" => 'DESC'),
'conditions' => array(),

// these will be part of the extra argument in your custom method
'statuses' => array(0,1)
),
// override the defaults above
$StatusReq = ( !is_null($status) ) ? explode('-', $status) : $this->paginate[$model_name]['statuses'];
$this->paginate[$model_name]['statuses'] = $StatusReq;

// on to the view
$this->set('RecordList', $this->paginate($model_name));
----
CategoryPastebin
Deletions:
/*
What happens here: model retrieves all records, then loops through them
to filter out the desired records based on the status values passed in
the extra array argument. Not the most efficient method, perhaps, but
certainly simpler than and safer than crafting a custom SQL query.

Note: the argument list is fixed by the CakePhp framework. Use the extra
argument if you need pass additional values.
*/
$Result = array();
$PreResult = $this->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));

foreach ( $PreResult as $Rec )
{
if ( in_array($Rec['ExtagMod'][0]['status'], $extra['statuses']) )
{
$Result[] = $Rec;
}
}
#pr(array('paginate result' => $Result));
return $Result;
$StatusList = ( isset($extra['statuses']) ) ? $extra['statuses'] : array(1);
if ( !is_array($StatusList) ) $StatusList = array( $StatusList );
$statuses = implode(',', $StatusList);

$sql = <<<MYSQL
$count = count($this->query($sql));
#pr(array('paginateCount result' => $count));
return $count;
$model_name => array
(
'recursive' => 2,
'limit' => 10,
'order' => array("$model_name.created" => 'DESC'),
'conditions' => array(),

// these will be part of the extra argument in your custom method
'statuses' => array(0,1)
),
// override the defaults above
$StatusReq = ( !is_null($status) ) ? explode('-', $status) : $this->paginate[$model_name]['statuses'];
$this->paginate[$model_name]['statuses'] = $StatusReq;

// on to the view
$this->set('RecordList', $this->paginate($model_name));


Revision [1018]

Edited on 2009-01-17 11:30:34 by KlenwellAdmin
Additions:
$this->set('RecordList', $this->paginate($model_name));
Deletions:
$this->set('TagList', $this->paginate('Extag'));


Revision [1017]

Edited on 2009-01-17 11:29:22 by KlenwellAdmin
Additions:
""$AssocHasManyModel"" in foreach loop below is the model associated with this model that has the field/column I want to filter on.
$assoc_hasmany_table in the ""paginateCount"" method is the table name for the associated model.
Replace $model_name with your model name.
var $paginate = array
(
$model_name => array
(
'recursive' => 2,
'limit' => 10,
'order' => array("$model_name.created" => 'DESC'),
'conditions' => array(),

// these will be part of the extra argument in your custom method
'statuses' => array(0,1)
),
);
// action method using paginator (simplified)
function overview($status=null)
{
// override the defaults above
$StatusReq = ( !is_null($status) ) ? explode('-', $status) : $this->paginate[$model_name]['statuses'];
$this->paginate[$model_name]['statuses'] = $StatusReq;
// on to the view
$this->set('TagList', $this->paginate('Extag'));
Deletions:
$AssocHasManyModel in foreach loop below is the model associated with this model that has the field/column I want to filter on.
$assoc_hasmany_table in the paginateCount method is the table name for the associated model.


Revision [1016]

Edited on 2009-01-17 11:21:34 by KlenwellAdmin
Additions:
$PreResult = $this->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
Deletions:
$PreResult = $this->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));


Revision [1015]

Edited on 2009-01-17 11:08:30 by KlenwellAdmin
Additions:
For more information: see http://www.klenwell.com/press/2009/01/cakephp-custom-pagination/
Deletions:
For more information: see


Revision [1014]

Edited on 2009-01-17 11:04:07 by KlenwellAdmin
Additions:
For more information: see
===Model Code===
$AssocHasManyModel in foreach loop below is the model associated with this model that has the field/column I want to filter on.
$assoc_hasmany_table in the paginateCount method is the table name for the associated model.
%%(php)
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array())
{
/*
What happens here: model retrieves all records, then loops through them
to filter out the desired records based on the status values passed in
the extra array argument. Not the most efficient method, perhaps, but
certainly simpler than and safer than crafting a custom SQL query.

Note: the argument list is fixed by the CakePhp framework. Use the extra
argument if you need pass additional values.
*/
$Result = array();
$PreResult = $this->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));

foreach ( $PreResult as $Rec )
{
if ( in_array($Rec['ExtagMod'][0]['status'], $extra['statuses']) )
{
$Result[] = $Rec;
}
}
#pr(array('paginate result' => $Result));
return $Result;
}


function paginateCount($conditions = null, $recursive = 0, $extra = array())
{
$StatusList = ( isset($extra['statuses']) ) ? $extra['statuses'] : array(1);
if ( !is_array($StatusList) ) $StatusList = array( $StatusList );
$statuses = implode(',', $StatusList);

$sql = <<<MYSQL
SELECT MAX( created )
FROM $assoc_hasmany_table
WHERE STATUS IN ( $statuses )
GROUP BY $assoc_hasmany_table_id_column
MYSQL;
$count = count($this->query($sql));
#pr(array('paginateCount result' => $count));
return $count;
}
%%
===Controller Code===
%%(php)
%%


Revision [1013]

The oldest known version of this page was created on 2009-01-17 10:52:12 by KlenwellAdmin