Files
pmgr/app_model.php

140 lines
4.8 KiB
PHP

<?php
/* SVN FILE: $Id: app_model.php 7945 2008-12-19 02:16:01Z gwoo $ */
/**
* Application model for Cake.
*
* This file is application-wide model file. You can put all
* application-wide model-related methods here.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.app
* @since CakePHP(tm) v 0.2.9
* @version $Revision: 7945 $
* @modifiedby $LastChangedBy: gwoo $
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Application model for Cake.
*
* Add your application-wide methods in the class below, your models
* will inherit them.
*
* @package cake
* @subpackage cake.app
*/
class AppModel extends Model {
//var $actsAs = array('Containable');
var $actsAs = array('Linkable');
/**
* Get Enum Values
* Snippet v0.1.3
* http://cakeforge.org/snippet/detail.php?type=snippet&id=112
*
* Gets the enum values for MySQL 4 and 5 to use in selectTag()
*/
function getEnumValues($columnName=null, $respectDefault=false)
{
if ($columnName==null) { return array(); } //no field specified
//Get the name of the table
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$tableName = $db->fullTableName($this, false);
//Get the values for the specified column (database and version specific, needs testing)
$result = $this->query("SHOW COLUMNS FROM {$tableName} LIKE '{$columnName}'");
//figure out where in the result our Types are (this varies between mysql versions)
$types = null;
if ( isset( $result[0]['COLUMNS']['Type'] ) ) { //MySQL 5
$types = $result[0]['COLUMNS']['Type']; $default = $result[0]['COLUMNS']['Default'];
}
elseif ( isset( $result[0][0]['Type'] ) ) { //MySQL 4
$types = $result[0][0]['Type']; $default = $result[0][0]['Default'];
}
else { //types return not accounted for
return array();
}
//Get the values
return array_flip(array_merge(array(''), // MySQL sets 0 to be the empty string
explode("','", preg_replace("/(enum)\('(.+?)'\)/","\\2", $types))
));
} //end getEnumValues
// Overriding pagination, since the stupid count mechanism blows.
// This is also a crappy solution, especially if the query returns
// a really large number of rows. However, trying to untagle this
// without changing a bunch of core code is near impossible.
var $paginate_rows_save;
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null) {
/* pr("paginate"); */
/* pr(array_merge(compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'))); */
if ($page > 1 && isset($limit))
$offset = ($page - 1) * $limit;
else
$offset = 0;
return array_slice($this->paginate_rows_save, $offset, $limit);
}
function paginateCount($conditions = null, $recursive = null, $extra = null) {
/* pr("paginateCount"); */
if (!isset($recursive))
$recursive = $this->recursive;
$parameters = array_merge(compact('conditions', 'recursive'), $extra);
$results = $this->find('all', $parameters);
$this->paginate_rows_save = $results;
return count($this->paginate_rows_save);
}
// Code as found in the controller
/* function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null) { */
/* pr("paginate"); */
/* pr(array_merge(compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'))); */
/* $parameters = compact('conditions', 'fields', 'order', 'limit', 'page'); */
/* if ($recursive != $this->recursive) { */
/* $parameters['recursive'] = $recursive; */
/* } */
/* $results = $this->find($type, array_merge($parameters, $extra)); */
/* pr("paginate end"); */
/* pr($results); */
/* } */
/* function paginateCount($conditions = null, $recursive = null, $extra = null) { */
/* pr("paginateCount"); */
/* pr(array_merge(compact('conditions', 'recursive'), $extra)); */
/* $parameters = compact('conditions'); */
/* if ($recursive != $this->recursive) { */
/* $parameters['recursive'] = $recursive; */
/* } */
/* $count = $this->find('count', array_merge($parameters, $extra)); */
/* pr("paginateCount end: $count"); */
/* return $count; */
/* } */
}