Added support for querying only occupied or vacant units. It's working at the moment, but I'll probably move some more logic to the controller next.
git-svn-id: file:///svn-source/pmgr/branches/initial_20090526@29 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -37,5 +37,42 @@
|
|||||||
* @subpackage cake.app
|
* @subpackage cake.app
|
||||||
*/
|
*/
|
||||||
class AppModel extends Model {
|
class AppModel extends Model {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
@@ -12,29 +12,13 @@ class UnitsController extends AppController {
|
|||||||
|
|
||||||
function vacant() {
|
function vacant() {
|
||||||
$this->Unit->recursive = 0;
|
$this->Unit->recursive = 0;
|
||||||
$this->Unit->bindModel(array('hasOne' => array('UnitsLease',
|
$this->set('units', $this->paginate(array('Unit.status < ' . $this->Unit->occupiedEnumValue())));
|
||||||
'Lease' => array(
|
|
||||||
'foreignKey' => false,
|
|
||||||
'conditions' => array('Lease.id = UnitsLease.lease_id')
|
|
||||||
))),
|
|
||||||
false);
|
|
||||||
|
|
||||||
$this->set('units', $this->paginate(array('Lease.close_date IS NULL',
|
|
||||||
'UnitsLease.type != "ALTERNATE"')));
|
|
||||||
$this->render('index');
|
$this->render('index');
|
||||||
}
|
}
|
||||||
|
|
||||||
function occupied() {
|
function occupied() {
|
||||||
$this->Unit->recursive = 0;
|
$this->Unit->recursive = 0;
|
||||||
$this->Unit->bindModel(array('hasOne' => array('UnitsLease',
|
$this->set('units', $this->paginate(array('Unit.status >= ' . $this->Unit->occupiedEnumValue())));
|
||||||
'Lease' => array(
|
|
||||||
'foreignKey' => false,
|
|
||||||
'conditions' => array('Lease.id = UnitsLease.lease_id')
|
|
||||||
))),
|
|
||||||
false);
|
|
||||||
|
|
||||||
$this->set('units', $this->paginate(array('Lease.close_date IS NULL',
|
|
||||||
'UnitsLease.type != "ALTERNATE"')));
|
|
||||||
$this->render('index');
|
$this->render('index');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,5 +45,8 @@ class Unit extends AppModel {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
function occupiedEnumValue() {
|
||||||
|
$enums = $this->getEnumValues('status');
|
||||||
|
return $enums['OCCUPIED'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
@@ -18,12 +18,12 @@
|
|||||||
'format' => __('Page %page% of %pages%, showing %current% records (%start% - %end%) of %count% total', true)));
|
'format' => __('Page %page% of %pages%, showing %current% records (%start% - %end%) of %count% total', true)));
|
||||||
|
|
||||||
$rows = array($paginator->sort('id'),
|
$rows = array($paginator->sort('id'),
|
||||||
$paginator->sort('name'),
|
$paginator->sort('Unit', 'name'),
|
||||||
$paginator->sort('unit_size_id'),
|
$paginator->sort('unit_size_id'),
|
||||||
$paginator->sort('status'),
|
$paginator->sort('status'),
|
||||||
$paginator->sort('comment'));
|
$paginator->sort('comment'));
|
||||||
} else {
|
} else {
|
||||||
$rows = array('Id', 'Name', 'Size', 'Status', 'Comment');
|
$rows = array('Id', 'Unit', 'Size', 'Status', 'Comment');
|
||||||
}
|
}
|
||||||
echo $html->tableHeaders($rows);
|
echo $html->tableHeaders($rows);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user