From 94e5730b4168aa24a22c7c65c3f314ad06fcc6a3 Mon Sep 17 00:00:00 2001 From: phpnut Date: Sat, 5 Nov 2005 20:35:43 +0000 Subject: [PATCH] [1335] Author: phpnut Date: 2:11:57 PM, Saturday, November 05, 2005 Message: Fixing Controller::generateFieldNames() to work with changes to Model [1334] Author: phpnut Date: 1:18:02 PM, Saturday, November 05, 2005 Message: More work on changes made in the model to allow association names to be used as an alias. These changes are being made to update scaffold to work with changes in [1330]. Added var $alias that holds an array with the key value pair of the table name and the association name. [1330] Author: phpnut Date: 10:25:16 PM, Friday, November 04, 2005 Message: Adding fix for Ticket #127. The query returns properly now but, this breaks scaffold which I will be fixing soon. Also the save methods for the model have not been tested but I am sure these need to be corrected also. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1336 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/controller/controller.php | 21 ++-- cake/libs/controller/scaffold.php | 36 +++--- .../controller/templates/scaffolds/list.thtml | 6 +- .../controller/templates/scaffolds/show.thtml | 28 ++--- cake/libs/model/model.php | 106 +++++++++--------- cake/libs/session.php | 2 + .../libs/view/templates/layouts/default.thtml | 2 +- 7 files changed, 106 insertions(+), 95 deletions(-) diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index c27fdfb55..cb9f564a9 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -564,6 +564,7 @@ class Controller extends Object $model = $this->modelClass; $modelKey = $this->modelKey; $table = $this->{$model}->table; + $association = array_search($table,$this->{$model}->alias); $classRegistry =& ClassRegistry::getInstance(); $objRegistryModel = $classRegistry->getObject($modelKey); @@ -580,12 +581,13 @@ class Controller extends Object $fieldNames[ $tabl['name'] ]['prompt'] = Inflector::humanize($niceName); // this is a foreign key, also set up the other controller $fieldNames[ $tabl['name'] ]['table'] = Inflector::pluralize($niceName); + $association = array_search($fieldNames[ $tabl['name'] ]['table'],$this->{$model}->alias); if($this->{$model}->tableToModel[$fieldNames[ $tabl['name'] ]['table']] == $model) { $alias = 'Child_'; } $fieldNames[ $tabl['name'] ]['prompt'] = Inflector::humanize($alias.$niceName); - $fieldNames[ $tabl['name'] ]['model'] = $alias.$this->{$model}->tableToModel[$fieldNames[ $tabl['name'] ]['table']]; + $fieldNames[ $tabl['name'] ]['model'] = $alias.$association; $fieldNames[ $tabl['name'] ]['modelKey'] = $this->{$model}->tableToModel[$fieldNames[ $tabl['name'] ]['table']]; $fieldNames[ $tabl['name'] ]['controller'] = Inflector::pluralize($this->{$model}->tableToModel[Inflector::pluralize($niceName)]); $fieldNames[ $tabl['name'] ]['foreignKey'] = true; @@ -666,14 +668,15 @@ class Controller extends Object { foreach( $pass as $key=>$value ) { - if( $alias.$key == $fieldNames[ $tabl['name']]['model'] && isset( $value['id'] ) && isset( $value[$otherDisplayField] ) ) + + if( $alias.$key == $this->{$model}->tableToModel[$fieldNames[ $tabl['name'] ]['table']] && isset( $value['id'] ) && isset( $value[$otherDisplayField] ) ) { $fieldNames[ $tabl['name']]['options'][$value['id']] = $value[$otherDisplayField]; } } } } - $fieldNames[ $tabl['name']]['selected'] = $data[$table][$tabl['name']]; + $fieldNames[ $tabl['name']]['selected'] = $data[$association][$tabl['name']]; } } else @@ -728,7 +731,7 @@ class Controller extends Object { foreach( $pass as $key=>$value ) { - if( $alias.$key == $fieldNames[ $tabl['name']]['model'] && isset( $value['id'] ) && isset( $value[$otherDisplayField] ) ) + if( $alias.$key == $this->{$model}->tableToModel[$fieldNames[ $tabl['name'] ]['table']] && isset( $value['id'] ) && isset( $value[$otherDisplayField] ) ) { $fieldNames[ $tabl['name']]['options'][$value['id']] = $value[$otherDisplayField]; } @@ -779,8 +782,8 @@ class Controller extends Object // loop through the many to many relations to make a list box. foreach( $objRegistryModel->_manyToMany as $relation ) { - list($modelName) = $relation; - + //list($modelName) = $relation; + list($manyAssociation, $modelName, $value) = $relation; $modelKeyM = Inflector::underscore($modelName); $modelObject = new $modelName(); @@ -790,7 +793,7 @@ class Controller extends Object $fieldNames[$modelKeyM]['model'] = $modelName; $fieldNames[$modelKeyM]['prompt'] = "Related ".Inflector::humanize(Inflector::pluralize($modelName)); $fieldNames[$modelKeyM]['type'] = "selectMultiple"; - $fieldNames[$modelKeyM]['tagName'] = $modelName.'/'.$modelName; + $fieldNames[$modelKeyM]['tagName'] = $manyAssociation.'/'.$manyAssociation; foreach( $modelObject->findAll() as $pass ) { @@ -802,9 +805,9 @@ class Controller extends Object } } } - if( isset( $data[$modelName] ) ) + if( isset( $data[$manyAssociation] ) ) { - foreach( $data[$modelName] as $key => $row ) + foreach( $data[$manyAssociation] as $key => $row ) { $fieldNames[$modelKeyM]['selected'][$row['id']] = $row['id']; } diff --git a/cake/libs/controller/scaffold.php b/cake/libs/controller/scaffold.php index f0328ea6d..9d83a8534 100644 --- a/cake/libs/controller/scaffold.php +++ b/cake/libs/controller/scaffold.php @@ -409,45 +409,45 @@ class Scaffold extends Object { */ function _cleanUpFields() { - $modelKey = Inflector::underscore($this->modelKey); + foreach( $this->controllerClass->{$this->modelKey}->_tableInfo as $table ) { foreach ($table as $field) { - if('date' == $field['type'] && isset($this->controllerClass->params['data'][$modelKey][$field['name'].'_year'])) + if('date' == $field['type'] && isset($this->controllerClass->params['data'][$this->modelKey][$field['name'].'_year'])) { $newDate = mktime( 0,0,0, - $this->controllerClass->params['data'][$modelKey][$field['name'].'_month'], - $this->controllerClass->params['data'][$modelKey][$field['name'].'_day'], - $this->controllerClass->params['data'][$modelKey][$field['name'].'_year'] ); + $this->controllerClass->params['data'][$this->modelKey][$field['name'].'_month'], + $this->controllerClass->params['data'][$this->modelKey][$field['name'].'_day'], + $this->controllerClass->params['data'][$this->modelKey][$field['name'].'_year'] ); $newDate = date( 'Y-m-d', $newDate ); - $this->controllerClass->params['data'][$modelKey][$field['name']] = $newDate; + $this->controllerClass->params['data'][$this->modelKey][$field['name']] = $newDate; } - else if( 'datetime' == $field['type'] && isset($this->controllerClass->params['data'][$modelKey][$field['name'].'_year'] ) ) + else if( 'datetime' == $field['type'] && isset($this->controllerClass->params['data'][$this->modelKey][$field['name'].'_year'] ) ) { - $hour = $this->controllerClass->params['data'][$modelKey][$field['name'].'_hour']; - if( $hour != 12 && 'pm' == $this->controllerClass->params['data'][$modelKey][$field['name'].'_meridian'] ) + $hour = $this->controllerClass->params['data'][$this->modelKey][$field['name'].'_hour']; + if( $hour != 12 && 'pm' == $this->controllerClass->params['data'][$this->modelKey][$field['name'].'_meridian'] ) { $hour = $hour + 12; } $newDate = mktime( $hour, - $this->controllerClass->params['data'][$modelKey][$field['name'].'_min'],0, - $this->controllerClass->params['data'][$modelKey][$field['name'].'_month'], - $this->controllerClass->params['data'][$modelKey][$field['name'].'_day'], - $this->controllerClass->params['data'][$modelKey][$field['name'].'_year'] ); + $this->controllerClass->params['data'][$this->modelKey][$field['name'].'_min'],0, + $this->controllerClass->params['data'][$this->modelKey][$field['name'].'_month'], + $this->controllerClass->params['data'][$this->modelKey][$field['name'].'_day'], + $this->controllerClass->params['data'][$this->modelKey][$field['name'].'_year'] ); $newDate = date( 'Y-m-d H:i:s', $newDate ); - $this->controllerClass->params['data'][$modelKey][$field['name']] = $newDate; + $this->controllerClass->params['data'][$this->modelKey][$field['name']] = $newDate; } else if( 'tinyint(1)' == $field['type'] ) { - if( isset( $this->controllerClass->params['data'][$modelKey][$field['name']]) && - "on" == $this->controllerClass->params['data'][$modelKey][$field['name']] ) + if( isset( $this->controllerClass->params['data'][$this->modelKey][$field['name']]) && + "on" == $this->controllerClass->params['data'][$this->modelKey][$field['name']] ) { - $this->controllerClass->params['data'][$modelKey][$field['name']] = true; + $this->controllerClass->params['data'][$this->modelKey][$field['name']] = true; } else { - $this->controllerClass->params['data'][$modelKey][$field['name']] = false; + $this->controllerClass->params['data'][$this->modelKey][$field['name']] = false; } } } diff --git a/cake/libs/controller/templates/scaffolds/list.thtml b/cake/libs/controller/templates/scaffolds/list.thtml index 2e1e533ee..467aec324 100644 --- a/cake/libs/controller/templates/scaffolds/list.thtml +++ b/cake/libs/controller/templates/scaffolds/list.thtml @@ -61,17 +61,17 @@ getObject( $otherModelKey ); + $alias = array_search($value['table'],$this->controller->{$model}->alias); if( is_object($otherModelObject) ) { - $displayText = $row[$value['model']][ $otherModelObject->getDisplayField() ]; + $displayText = $row[$alias][ $otherModelObject->getDisplayField() ]; } else{ - $displayText = $row[$value['model']][$field]; + $displayText = $row[$alias][$field]; } echo $html->linkTo( $displayText, "/".Inflector::underscore($otherControllerName)."/show/".$row[$modelKey][$field] ); diff --git a/cake/libs/controller/templates/scaffolds/show.thtml b/cake/libs/controller/templates/scaffolds/show.thtml index 284eda4b6..962225516 100644 --- a/cake/libs/controller/templates/scaffolds/show.thtml +++ b/cake/libs/controller/templates/scaffolds/show.thtml @@ -44,10 +44,10 @@ echo "
".$value['prompt']."
"; if(isset($value['foreignKey'])) { - + $alias = array_search($value['table'],$objModel->alias); $otherModelObject = $registry->getObject(Inflector::underscore($objModel->tableToModel[$value['table']])); $displayField = $otherModelObject->getDisplayField(); - $displayText = $data[$objModel->tableToModel[$value['table']]][$displayField]; + $displayText = $data[$alias][$displayField]; if(!empty($data[$objModel->tableToModel[$objModel->table]][$field])) { @@ -84,7 +84,7 @@ foreach( $fieldNames as $field => $value ) { if( isset( $value['foreignKey'] ) ) { - echo "
  • ".$html->linkTo( "View ".Inflector::humanize($value['controller']), "/".Inflector::underscore($value['controller'])."/show/".$data[$objModel->tableToModel[$value['table']]]['id'] )."
  • "; + echo "
  • ".$html->linkTo( "View ".Inflector::humanize($value['controller']), "/".Inflector::underscore($value['controller'])."/show/".$data[$alias]['id'] )."
  • "; } } ?> @@ -94,15 +94,15 @@ _oneToOne as $relation) { - list($model, $value) = $relation; + list($association, $model, $value) = $relation; $otherModelName = $objModel->tableToModel[$model]; $controller = Inflector::pluralize($model); - echo ""; } ?> @@ -134,13 +134,13 @@ foreach( $relations as $relation ) { - list($model, $value) = $relation; + list($association, $model, $value) = $relation; $count = 0; $otherModelName = Inflector::singularize($model); $controller = Inflector::pluralize($model); - echo "