Merging fixes and enhancements into trunk

Revision: [1855]
Forgot to merge changes between the PHP 5 and PHP 4 model classes

Revision: [1854]
Added fix to Model class so that setting var $useTable = false; will not try to create a datasource connection.
Added fix to basics.php in the file_put_contents function that would give a Warning :failed to open stream: File exists. 
This fix allows the file to be wrote to if it already exists.
Added fix for undefined notice in DboSource::generateAssociationQuery();
Added Model::$keyToTable to map foreign keys to thier proper tables so scaffold could work with them properly.
Fixed Controller::__generateAssociation() to use Model::$keyToTable.
Fixed layout issue with show view in scaffold.

Revision: [1853]
Added DataSource::buildSchemaQuery() which must be implemented in the child classes.
  This will be used to import a table schema.
Added DboMysql::buildSchemaQuery()
Added fix for Ticket #304 by reverting change made in [1825].
Reopened Ticket #286
Added fix for Ticket #310 

Revision: [1852]
Added fix for PHP Fatal error: Call to a member function on a non-object in... Controller::generateFieldNames() in PHP 4            
when using a HABTM association. 

git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1856 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2006-01-22 05:12:28 +00:00
parent 660259640d
commit 26d7620f1b
10 changed files with 124 additions and 86 deletions

View file

@ -6,4 +6,4 @@
// +---------------------------------------------------------------------------------------------------+ // // +---------------------------------------------------------------------------------------------------+ //
/////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
0.10.7.1851 RC 3 0.10.7.1856 RC 3

View file

@ -692,7 +692,7 @@ if (!function_exists('file_put_contents'))
{ {
$data = join('', $data); $data = join('', $data);
} }
$res = @fopen($fileName, 'xb'); $res = @fopen($fileName, 'w+b');
if ($res) if ($res)
{ {
@fwrite($res, $data); @fwrite($res, $data);

View file

@ -494,13 +494,14 @@ class Controller extends Object
if ($objRegistryModel->isForeignKey($tabl['name'])) if ($objRegistryModel->isForeignKey($tabl['name']))
{ {
$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); $fkNames = $this->{$model}->keyToTable[$tabl['name']];
$fieldNames[ $tabl['name'] ]['table'] = Inflector::pluralize($niceName); $fieldNames[ $tabl['name'] ]['prompt'] = Inflector::humanize($fkNames[1]);
$fieldNames[ $tabl['name'] ]['table'] = $fkNames[0];
$association = array_search($fieldNames[ $tabl['name'] ]['table'],$this->{$model}->alias); $association = array_search($fieldNames[ $tabl['name'] ]['table'],$this->{$model}->alias);
$fieldNames[ $tabl['name'] ]['prompt'] = Inflector::humanize($alias.$niceName); $fieldNames[ $tabl['name'] ]['prompt'] = Inflector::humanize($alias.$niceName);
$fieldNames[ $tabl['name'] ]['model'] = $alias.$association; $fieldNames[ $tabl['name'] ]['model'] = $fkNames[1];
$fieldNames[ $tabl['name'] ]['modelKey'] = $this->{$model}->tableToModel[$fieldNames[ $tabl['name'] ]['table']]; $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'] ]['controller'] = Inflector::pluralize($this->{$model}->tableToModel[Inflector::pluralize($fkNames[0])]);
$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'] )
@ -660,7 +661,7 @@ class Controller extends Object
$modelName = $relData['className']; $modelName = $relData['className'];
$manyAssociation = $relation; $manyAssociation = $relation;
$modelKeyM = Inflector::underscore($modelName); $modelKeyM = Inflector::underscore($modelName);
$modelObject = new $modelName(); $modelObject =& new $modelName();
if($doCreateOptions) if($doCreateOptions)
{ {
$otherDisplayField = $modelObject->getDisplayField(); $otherDisplayField = $modelObject->getDisplayField();

View file

@ -508,6 +508,11 @@ class DataSource extends Object
return $data; return $data;
} }
function buildSchemaQuery($schema)
{
die("Implement in DBO");
}
/** /**
* Enter description here... * Enter description here...
* *

View file

@ -593,10 +593,14 @@ class DboSource extends DataSource
case 'hasOne': case 'hasOne':
if ($external) if ($external)
{ {
if ($assocData['finderQuery']) if (isset($assocData['finderQuery']))
{ {
return $assocData['finderQuery']; return $assocData['finderQuery'];
} }
if(!isset($assocData['fields']))
{
$assocData['fields'] = '';
}
$sql = 'SELECT '.join(', ', $this->fields($linkModel, $alias, $assocData['fields'])); $sql = 'SELECT '.join(', ', $this->fields($linkModel, $alias, $assocData['fields']));
$sql .= ' FROM '.$this->name($linkModel->table).' AS '.$alias; $sql .= ' FROM '.$this->name($linkModel->table).' AS '.$alias;
$conditions = $queryData['conditions']; $conditions = $queryData['conditions'];

View file

@ -285,14 +285,7 @@ class DboMysql extends DboSource
{ {
$data = mysql_real_escape_string($data, $this->connection); $data = mysql_real_escape_string($data, $this->connection);
} }
if($data != '')
{
$return = "'" . $data . "'"; $return = "'" . $data . "'";
}
else
{
$return = "NULL";
}
return $return; return $return;
} }
@ -536,5 +529,16 @@ class DboMysql extends DboSource
return false; return false;
} }
} }
function buildSchemaQuery($schema)
{
$search = array('{AUTOINCREMENT}', '{PRIMARY}', '{UNSIGNED}', '{FULLTEXT}',
'{FULLTEXT_MYSQL}', '{BOOLEAN}', '{UTF_8}');
$replace = array('int(11) not null auto_increment', 'primary key', 'unsigned',
'FULLTEXT', 'FULLTEXT', 'enum (\'true\', \'false\') NOT NULL default \'true\'',
'/*!40100 CHARACTER SET utf8 COLLATE utf8_unicode_ci */');
$query = trim(str_replace($search, $replace, $schema));
return $query;
}
} }
?> ?>

View file

@ -181,6 +181,13 @@ class Model extends Object
*/ */
var $modelToTable = array(); var $modelToTable = array();
/**
* List of Foreign Key names to table used tables. Used for associations.
*
* @var array
*/
var $keyToTable = array();
/** /**
* Alias table names for model, for use in SQL JOIN statements. * Alias table names for model, for use in SQL JOIN statements.
* *
@ -300,13 +307,13 @@ class Model extends Object
} }
$this->currentModel = Inflector::underscore($this->name); $this->currentModel = Inflector::underscore($this->name);
$this->setDataSource($ds);
ClassRegistry::addObject($this->currentModel, $this); ClassRegistry::addObject($this->currentModel, $this);
$this->id = $id; $this->id = $id;
if($this->useTable !== false) if($this->useTable !== false)
{ {
$this->setDataSource($ds);
if ($table) if ($table)
{ {
$tableName = $table; $tableName = $table;
@ -336,8 +343,8 @@ class Model extends Object
{ {
$this->setSource($tableName); $this->setSource($tableName);
} }
$this->__createLinks(); $this->__createLinks();
}
if ($this->displayField == null) if ($this->displayField == null)
{ {
@ -355,8 +362,7 @@ class Model extends Object
} }
} }
} }
}
/** /**
* PHP4 Only * PHP4 Only
@ -489,6 +495,11 @@ class Model extends Object
} }
$this->{$type}[$assocKey][$key] = $data; $this->{$type}[$assocKey][$key] = $data;
} }
if($key == 'foreignKey' && !isset($this->keyToTable[$this->{$type}[$assocKey][$key]]))
{
$this->keyToTable[$this->{$type}[$assocKey][$key]][0] = $this->{$class}->table;
$this->keyToTable[$this->{$type}[$assocKey][$key]][1] = $this->{$class}->name;
}
} }
} }
} }

View file

@ -181,6 +181,13 @@ class Model extends Object
*/ */
var $modelToTable = array(); var $modelToTable = array();
/**
* List of Foreign Key names to table used tables. Used for associations.
*
* @var array
*/
var $keyToTable = array();
/** /**
* Alias table names for model, for use in SQL JOIN statements. * Alias table names for model, for use in SQL JOIN statements.
* *
@ -300,13 +307,13 @@ class Model extends Object
} }
$this->currentModel = Inflector::underscore($this->name); $this->currentModel = Inflector::underscore($this->name);
$this->setDataSource($ds);
ClassRegistry::addObject($this->currentModel, $this); ClassRegistry::addObject($this->currentModel, $this);
$this->id = $id; $this->id = $id;
if($this->useTable !== false) if($this->useTable !== false)
{ {
$this->setDataSource($ds);
if ($table) if ($table)
{ {
$tableName = $table; $tableName = $table;
@ -336,8 +343,8 @@ class Model extends Object
{ {
$this->setSource($tableName); $this->setSource($tableName);
} }
$this->__createLinks(); $this->__createLinks();
}
if ($this->displayField == null) if ($this->displayField == null)
{ {
@ -355,6 +362,7 @@ class Model extends Object
} }
} }
} }
}
/** /**
* Handles custom method calls, like findBy<field> for DB models, * Handles custom method calls, like findBy<field> for DB models,
@ -483,6 +491,11 @@ class Model extends Object
} }
$this->{$type}[$assocKey][$key] = $data; $this->{$type}[$assocKey][$key] = $data;
} }
if($key == 'foreignKey' && !isset($this->keyToTable[$this->{$type}[$assocKey][$key]]))
{
$this->keyToTable[$this->{$type}[$assocKey][$key]][0] = $this->{$class}->table;
$this->keyToTable[$this->{$type}[$assocKey][$key]][1] = $this->{$class}->name;
}
} }
} }
} }

View file

@ -215,7 +215,7 @@ class AjaxHelper extends Helper
if (isset($options['before'])) if (isset($options['before']))
{ {
$func = "{$options['before']}; $function"; $func = "{$options['before']}; $func";
} }
if (isset($options['after'])) if (isset($options['after']))
{ {

View file

@ -46,7 +46,7 @@
$displayField = $otherModelObject->getDisplayField(); $displayField = $otherModelObject->getDisplayField();
$displayText = $data[$alias][$displayField]; $displayText = $data[$alias][$displayField];
if(!empty($data[$objModel->tableToModel[$objModel->table]][$field])) if(!empty($data[$objModel->tableToModel[$objModel->table]][$field]) && (isset($displayText)))
{ {
echo "<dd>".$html->link($displayText, '/'.Inflector::underscore($value['controller']).'/show/' echo "<dd>".$html->link($displayText, '/'.Inflector::underscore($value['controller']).'/show/'
.$data[$objModel->tableToModel[$objModel->table]][$field] )."</dd>"; .$data[$objModel->tableToModel[$objModel->table]][$field] )."</dd>";