Author: phpnut
Date: 1:39:26 PM, Saturday, October 22, 2005
Message:
Just about done with refactoring the model class.
This should be tested to make sure all changes are working as expected.

[1158]
Author: phpnut
Date: 5:34:41 AM, Saturday, October 22, 2005
Message:
More work on associations.
Adding fields setting, order by and conditions to hasMany and hasAndBelongsToMany

[1157]
Author: phpnut
Date: 3:39:13 AM, Saturday, October 22, 2005
Message:
More cleanup in Model class.   

[1156]
Author: phpnut
Date: 2:43:38 AM, Saturday, October 22, 2005
Message:
Removing duplicate code the the associations.
Added Model::_associationSwitch(); to move all association settings to one location and remove duplicate code.

[1155]
Author: phpnut
Date: 1:52:47 AM, Saturday, October 22, 2005
Message:
More cleaning up of the model class

[1154]
Author: phpnut
Date: 1:40:34 AM, Saturday, October 22, 2005
Message:
Cleaning up code layout

[1153]
Author: phpnut
Date: 1:32:05 AM, Saturday, October 22, 2005
Message:
removing temp variables and extra calls to Inflector::underscore();

[1152]
Author: phpnut
Date: 1:22:58 AM, Saturday, October 22, 2005
Message:
More work on associations.
Removing code that is no longer needed.

[1151]
Author: phpnut
Date: 12:02:43 AM, Saturday, October 22, 2005
Message:
more work on associations

[1150]
Author: phpnut
Date: 6:25:45 PM, Friday, October 21, 2005
Message:
more refactoring of associations

[1149]
Author: phpnut
Date: 6:04:18 PM, Friday, October 21, 2005
Message:
refactoring model and adding more association code

[1145]
Author: phpnut
Date: 2:43:05 PM, Thursday, October 20, 2005
Message:
more refactoring on associations

[1143]
Author: phpnut
Date: 1:44:42 PM, Thursday, October 20, 2005
Message:
Refactoring associations code.
Starting work allowing full use of associations array settings


git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1160 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2005-10-22 19:31:38 +00:00
parent 6baaa1120f
commit 2bb31637cf
5 changed files with 828 additions and 1009 deletions

View file

@ -593,13 +593,15 @@ class Controller extends Object
$fieldNames = array(); $fieldNames = array();
// figure out what model and table we are working with // figure out what model and table we are working with
$model = Inflector::pluralize($this->name); $model = Inflector::underscore(Inflector::singularize($this->name));
$table = Inflector::underscore(Inflector::singularize($this->name)); $table = $this->models[$model]->table;
// get all of the column names. // get all of the column names.
$classRegistry =& ClassRegistry::getInstance(); $classRegistry =& ClassRegistry::getInstance();
$objRegistryModel = $classRegistry->getObject(Inflector::singularize($model)); $objRegistryModel = $classRegistry->getObject($model);
foreach ($objRegistryModel->_tableInfo as $tables) foreach ($objRegistryModel->_tableInfo as $tables)
{ {
foreach ($tables as $tabl) foreach ($tables as $tabl)
@ -610,8 +612,9 @@ class Controller extends Object
$niceName = substr( $tabl['name'], 0, strpos( $tabl['name'], "_id" ) ); $niceName = substr( $tabl['name'], 0, strpos( $tabl['name'], "_id" ) );
$fieldNames[ $tabl['name'] ]['prompt'] = Inflector::humanize($niceName); $fieldNames[ $tabl['name'] ]['prompt'] = Inflector::humanize($niceName);
// this is a foreign key, also set up the other controller // this is a foreign key, also set up the other controller
$fieldNames[ $tabl['name'] ]['model'] = Inflector::singularize($niceName); $fieldNames[ $tabl['name'] ]['table'] = Inflector::pluralize($niceName);
$fieldNames[ $tabl['name'] ]['controller'] = Inflector::pluralize($niceName); $fieldNames[ $tabl['name'] ]['model'] = $this->models[$model]->tableToModel[$fieldNames[ $tabl['name'] ]['table']];
$fieldNames[ $tabl['name'] ]['controller'] = Inflector::pluralize($this->models[$model]->tableToModel[Inflector::pluralize($niceName)]);
$fieldNames[ $tabl['name'] ]['foreignKey'] = true; $fieldNames[ $tabl['name'] ]['foreignKey'] = true;
} }
else if( 'created' != $tabl['name'] && 'updated' != $tabl['name'] ) else if( 'created' != $tabl['name'] && 'updated' != $tabl['name'] )
@ -629,7 +632,7 @@ class Controller extends Object
// Now, set up some other attributes that will be useful for auto generating a form. // Now, set up some other attributes that will be useful for auto generating a form.
//tagName is in the format table/field "post/title" //tagName is in the format table/field "post/title"
$fieldNames[ $tabl['name']]['tagName'] = $table.'/'.$tabl['name']; $fieldNames[ $tabl['name']]['tagName'] = $model.'/'.$tabl['name'];
// Now, find out if this is a required field. // Now, find out if this is a required field.
//$validationFields = $classRegistry->getObject($table)->validate; //$validationFields = $classRegistry->getObject($table)->validate;
@ -676,7 +679,7 @@ class Controller extends Object
// get the list of options from the other model. // get the list of options from the other model.
$registry =& ClassRegistry::getInstance(); $registry =& ClassRegistry::getInstance();
$otherModel = $registry->getObject($fieldNames[ $tabl['name']]['model']); $otherModel =& $registry->getObject($fieldNames[ $tabl['name']]['model']);
if( is_object($otherModel) ) if( is_object($otherModel) )
{ {
@ -738,7 +741,7 @@ class Controller extends Object
// get the list of options from the other model. // get the list of options from the other model.
$registry =& ClassRegistry::getInstance(); $registry =& ClassRegistry::getInstance();
$otherModel = $registry->getObject($fieldNames[ $tabl['name']]['model']); $otherModel =& $registry->getObject($fieldNames[ $tabl['name']]['model']);
if( is_object($otherModel) ) if( is_object($otherModel) )
{ {
@ -756,7 +759,7 @@ class Controller extends Object
} }
} }
} }
$fieldNames[ $tabl['name']]['selected'] = $data[$table][$tabl['name']]; $fieldNames[ $tabl['name']]['selected'] = $data[$model][$tabl['name']];
} }
} }
else else

View file

@ -32,6 +32,7 @@
<?php <?php
echo $html->formTag('/'.Inflector::underscore($this->name).'/update'); echo $html->formTag('/'.Inflector::underscore($this->name).'/update');
echo $form->generateFields( $fieldNames ); echo $form->generateFields( $fieldNames );

View file

@ -68,11 +68,11 @@
$otherModelName = $value['model']; $otherModelName = $value['model'];
$otherControllerName = $value['controller']; $otherControllerName = $value['controller'];
$registry = ClassRegistry::getInstance(); $registry =& ClassRegistry::getInstance();
$otherModelObject = $registry->getObject( $otherModelName ); $otherModelObject =& $registry->getObject( $otherModelName );
if( is_object($otherModelObject) ) if( is_object($otherModelObject) )
{ {
$displayText = $row[$otherModelName][ $otherModelObject->getDisplayField() ]; $displayText = $row[$value['model']][ $otherModelObject->getDisplayField() ];
} else{ } else{
$displayText = $row[$modelKey][$field]; $displayText = $row[$modelKey][$field];
} }

View file

@ -34,51 +34,55 @@
<?php <?php
$modelName = Inflector::singularize($this->name); $modelName = Inflector::singularize($this->name);
$modelKey = Inflector::underscore($modelName); $modelKey = Inflector::underscore($modelName);
$registry = ClassRegistry::getInstance(); $registry =& ClassRegistry::getInstance();
$objModel = $registry->getObject($modelName);
?> ?>
<dl> <dl>
<?php foreach( $fieldNames as $field=>$value ) { <?php foreach($fieldNames as $field => $value)
echo "<dt>".$value['prompt']."</dt>";
if( isset( $value['foreignKey'] ) ) {
$otherModelObject = $registry->getObject($value['model']);
$displayField = $otherModelObject->getDisplayField();
$displayText = $data[$value['model']][ $displayField ];
if( !empty($data[$modelKey][$field]))
{
echo "<dd>".$html->linkTo($displayText, '/'.Inflector::underscore($value['controller']).'/show/'.$data[$modelKey][ $field ] )."</dd>";
}
else
{
echo "<dd>&nbsp;</dd>";
}
}
else
{ {
// this is just a plain old field. echo "<dt>".$value['prompt']."</dt>";
if( !empty($data[$modelKey][$field])) if(isset($value['foreignKey']))
{ {
echo "<dd>".$data[$modelKey][$field]."</dd>"; $otherModelObject = $registry->getObject($objModel->tableToModel[$value['table']]);
$displayField = $otherModelObject->getDisplayField();
$displayText = $data[$objModel->tableToModel[$value['table']]][$displayField];
if(!empty($data[$objModel->tableToModel[$objModel->table]][$field]))
{
echo "<dd>".$html->linkTo($displayText, '/'.Inflector::underscore($value['controller']).'/show/'
.$data[$objModel->tableToModel[$objModel->table]][$field] )."</dd>";
}
else
{
echo "<dd>&nbsp;</dd>";
}
} }
else else
{ {
echo "<dd>&nbsp;</dd>"; // this is just a plain old field.
if( !empty($data[$objModel->tableToModel[$objModel->table]][$field]))
{
echo "<dd>".$data[$objModel->tableToModel[$objModel->table]][$field]."</dd>";
}
else
{
echo "<dd>&nbsp;</dd>";
}
} }
} }
}
?> ?>
</dl> </dl>
<ul class='actions'> <ul class='actions'>
<?php <?php
echo "<li>".$html->linkTo('Edit '.Inflector::humanize($modelKey), '/'.$this->viewPath.'/edit/'.$data[$modelKey]['id'])."</li>"; echo "<li>".$html->linkTo('Edit '.Inflector::humanize($objModel->name), '/'.$this->viewPath.'/edit/'.$data[$objModel->tableToModel[$objModel->table]]['id'])."</li>";
echo "<li>".$html->linkTo('Delete '.Inflector::humanize($modelKey), '/'.$this->viewPath.'/destroy/'.$data[$modelKey]['id'])."</li>"; echo "<li>".$html->linkTo('Delete '.Inflector::humanize($objModel->name), '/'.$this->viewPath.'/destroy/'.$data[$objModel->tableToModel[$objModel->table]]['id'])."</li>";
foreach( $fieldNames as $field=>$value ) { foreach( $fieldNames as $field => $value ) {
if( isset( $value['foreignKey'] ) ) if( isset( $value['foreignKey'] ) )
{ {
echo "<li>".$html->linkTo( "View ".Inflector::humanize($value['controller']), "/".Inflector::underscore($value['controller'])."/show/".$data[Inflector::singularize($params['controller'])][$field] )."</li>"; echo "<li>".$html->linkTo( "View ".Inflector::humanize($value['controller']), "/".Inflector::underscore($value['controller'])."/show/".$data[$objModel->tableToModel[$value['table']]]['id'] )."</li>";
} }
} }
?> ?>
@ -86,12 +90,10 @@
<!--hasOne relationships --> <!--hasOne relationships -->
<?php <?php
$objModel = $registry->getObject($modelName);
foreach ($objModel->_oneToOne as $relation) foreach ($objModel->_oneToOne as $relation)
{ {
list($table, $field, $value) = $relation; list($table, $field, $value) = $relation;
$otherModelName = Inflector::singularize($table); $otherModelName = $objModel->tableToModel[$table];
echo "<div class='related'><H2>Related ".Inflector::humanize($otherModelName)."</H2><br/><br/>"; echo "<div class='related'><H2>Related ".Inflector::humanize($otherModelName)."</H2><br/><br/>";
echo "<dl>"; echo "<dl>";
@ -129,12 +131,13 @@
foreach( $relations as $relation ) foreach( $relations as $relation )
{ {
list($table, $field, $value) = $relation; list($model, $value) = $relation;
$count = 0; $count = 0;
$otherModelName = Inflector::singularize($table); $otherModelName = Inflector::singularize($model);
$controller = Inflector::pluralize($model);
echo "<div class='related'><H2>Related ".Inflector::humanize($table)."</H2><br/><br/>"; echo "<div class='related'><H2>Related ".Inflector::humanize($objModel->tableToModel[$objModel->{$model}->table])."</H2><br/><br/>";
if( isset($data[$table]) && is_array($data[$table]) ) if( isset($data[$objModel->tableToModel[$objModel->{$model}->table]]) && is_array($data[$objModel->tableToModel[$objModel->{$model}->table]]) )
{ {
?> ?>
@ -143,7 +146,7 @@
<?php // Loop through and create the header row. <?php // Loop through and create the header row.
// find a row that matches this title. // find a row that matches this title.
$bFound = false; $bFound = false;
foreach( $data[$table][0] as $column=>$value ) { foreach( $data[$objModel->tableToModel[$objModel->{$model}->table]][0] as $column=>$value ) {
echo "<th>".Inflector::humanize($column)."</th>"; echo "<th>".Inflector::humanize($column)."</th>";
} }
?> ?>
@ -151,7 +154,7 @@
</tr> </tr>
<?php <?php
// now find all matching rows // now find all matching rows
foreach( $data[$table] as $row ) foreach( $data[$objModel->tableToModel[$objModel->$model->table]] as $row )
{ {
echo "<tr>"; echo "<tr>";
foreach( $row as $column=>$value ) foreach( $row as $column=>$value )
@ -159,9 +162,9 @@
echo "<td>".$value."</td>"; echo "<td>".$value."</td>";
} }
?> ?>
<td class="listactions"><?php echo $html->linkTo('View',"/".Inflector::underscore($table)."/show/{$row['id']}/")?> <td class="listactions"><?php echo $html->linkTo('View',"/".Inflector::underscore($controller)."/show/{$row['id']}/")?>
<?php echo $html->linkTo('Edit',"/".Inflector::underscore($table)."/edit/{$row['id']}/")?> <?php echo $html->linkTo('Edit',"/".Inflector::underscore($controller)."/edit/{$row['id']}/")?>
<?php echo $html->linkTo('Delete',"/".Inflector::underscore($table)."/destroy/{$row['id']}/")?> <?php echo $html->linkTo('Delete',"/".Inflector::underscore($controller)."/destroy/{$row['id']}/")?>
</td> </td>
<?php <?php
echo "</tr>"; echo "</tr>";
@ -174,7 +177,7 @@
<?php <?php
// add a link to create a new relation. // add a link to create a new relation.
echo "<li>".$html->linkTo('New '.Inflector::humanize($otherModelName),"/".Inflector::underscore($table)."/new/")."</li>"; echo "<li>".$html->linkTo('New '.Inflector::humanize($otherModelName),"/".Inflector::underscore($controller)."/new/")."</li>";
// echo "<li>".$html->linkTo( "View ".Inflector::humanize($table), "/".Inflector::underscore($table)."/list/".$modelName."/".$data[$modelName]['id'])."</li>"; // echo "<li>".$html->linkTo( "View ".Inflector::humanize($table), "/".Inflector::underscore($table)."/list/".$modelName."/".$data[$modelName]['id'])."</li>";
?> ?>
</ul></div> </ul></div>

File diff suppressed because it is too large Load diff