Fixing bug when instances of an HABTM association was automatically created.

Deprecated using $this->(ModelName) if an associations is created with an alias.

 
 	example with alias: var $hasMany = array('Assoc' => array('className' => 'ModelName'));
 					correct usage: $this->Assoc->modelMethods();

  	example without alias: var $hasMany = array('ModelName');
 					correct usage: $this->ModelName->modelMethods();

Fixed instances of 'className' that was still used in the core.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5608 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-08-29 20:47:03 +00:00
parent e7921c7741
commit f1de783295
2 changed files with 17 additions and 19 deletions

View file

@ -624,13 +624,7 @@ class DboSource extends DataSource {
$count2 = count($keys); $count2 = count($keys);
for ($j = 0; $j < $count2; $j++) { for ($j = 0; $j < $count2; $j++) {
$className = $key = $keys[$j];
$key = $keys[$j];
if (isset($associations[$key])) {
$className = $associations[$key]['className'];
} else {
$className = $key;
}
if ($model->name != $className && !in_array($key, $filtered)) { if ($model->name != $className && !in_array($key, $filtered)) {
if (!in_array($key, $filtering)) { if (!in_array($key, $filtering)) {
@ -698,7 +692,7 @@ class DboSource extends DataSource {
foreach ($linkModel->__associations as $type1) { foreach ($linkModel->__associations as $type1) {
foreach ($linkModel->{$type1} as $assoc1 => $assocData1) { foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
$deepModel =& $linkModel->{$assocData1['className']}; $deepModel =& $linkModel->{$assoc1};
if ($deepModel->alias != $model->name) { if ($deepModel->alias != $model->name) {
$tmpStack = $stack; $tmpStack = $stack;
$tmpStack[] = $assoc1; $tmpStack[] = $assoc1;
@ -749,7 +743,7 @@ class DboSource extends DataSource {
foreach ($linkModel->__associations as $type1) { foreach ($linkModel->__associations as $type1) {
foreach ($linkModel->{$type1} as $assoc1 => $assocData1) { foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
$deepModel =& $linkModel->{$assocData1['className']}; $deepModel =& $linkModel->{$assoc1};
if ($deepModel->alias != $model->name) { if ($deepModel->alias != $model->name) {
$tmpStack = $stack; $tmpStack = $stack;
$tmpStack[] = $assoc1; $tmpStack[] = $assoc1;

View file

@ -580,8 +580,6 @@ class Model extends Overloadable {
* @access private * @access private
*/ */
function __createLinks() { function __createLinks() {
// Convert all string-based associations to array based
foreach ($this->__associations as $type) { foreach ($this->__associations as $type) {
if (!is_array($this->{$type})) { if (!is_array($this->{$type})) {
$this->{$type} = explode(',', $this->{$type}); $this->{$type} = explode(',', $this->{$type});
@ -617,6 +615,12 @@ class Model extends Overloadable {
* @param mixed $id Primary key ID of linked model * @param mixed $id Primary key ID of linked model
* @param string $table Database table associated with linked model * @param string $table Database table associated with linked model
* @param string $ds Name of DataSource the model should be bound to * @param string $ds Name of DataSource the model should be bound to
* @deprecated $this->$className use $this->$assoc instead. $assoc is the 'key' in the associations array;
* examples: var $hasMany = array('Assoc' => array('className' => 'ModelName'));
* usage: $this->Assoc->modelMethods();
*
* var $hasMany = array('ModelName');
* usage: $this->ModelName->modelMethods();
* @access private * @access private
*/ */
function __constructLinkedModel($assoc, $className = null, $id = false, $table = null, $ds = null) { function __constructLinkedModel($assoc, $className = null, $id = false, $table = null, $ds = null) {
@ -634,20 +638,16 @@ class Model extends Overloadable {
if (ClassRegistry::isKeySet($colKey)) { if (ClassRegistry::isKeySet($colKey)) {
if (!PHP5) { if (!PHP5) {
$this->{$assoc} =& ClassRegistry::getObject($colKey); $this->{$assoc} =& ClassRegistry::getObject($colKey);
$this->{$className} =& $this->{$assoc};
ClassRegistry::map($assoc, $colKey); ClassRegistry::map($assoc, $colKey);
} else { } else {
$this->{$assoc} = ClassRegistry::getObject($colKey); $this->{$assoc} = ClassRegistry::getObject($colKey);
$this->{$className} = $this->{$assoc};
ClassRegistry::map($assoc, $colKey); ClassRegistry::map($assoc, $colKey);
} }
} else { } else {
if (!PHP5) { if (!PHP5) {
$this->{$assoc} =& new $className($id, $table, $ds); $this->{$assoc} =& new $className($id, $table, $ds);
$this->{$className} =& $this->{$assoc};
} else { } else {
$this->{$assoc} = new $className($id, $table, $ds); $this->{$assoc} = new $className($id, $table, $ds);
$this->{$className} = $this->{$assoc};
} }
} }
@ -713,7 +713,11 @@ class Model extends Overloadable {
if (isset($this->{$type}[$assocKey]['with']) && !empty($this->{$type}[$assocKey]['with'])) { if (isset($this->{$type}[$assocKey]['with']) && !empty($this->{$type}[$assocKey]['with'])) {
$joinClass = $this->{$type}[$assocKey]['with']; $joinClass = $this->{$type}[$assocKey]['with'];
if (!loadModel($joinClass)) { if (!loadModel($joinClass)) {
$this->__constructLinkedModel($joinClass, 'AppModel', false, $this->{$type}[$assocKey]['joinTable'], $this->useDbConfig); $this->{$joinClass} = new AppModel(array(
'name' => $joinClass,
'table' => $this->{$type}[$assocKey]['joinTable'],
'ds' => $this->useDbConfig
));
$this->{$joinClass}->name = $joinClass; $this->{$joinClass}->name = $joinClass;
$this->{$joinClass}->primaryKey = $this->{$type}[$assocKey]['foreignKey']; $this->{$joinClass}->primaryKey = $this->{$type}[$assocKey]['foreignKey'];
@ -724,6 +728,7 @@ class Model extends Overloadable {
} }
} else { } else {
$this->__constructLinkedModel($joinClass); $this->__constructLinkedModel($joinClass);
$this->{$joinClass}->name = $joinClass;
$this->{$type}[$assocKey]['joinTable'] = $this->{$joinClass}->table; $this->{$type}[$assocKey]['joinTable'] = $this->{$joinClass}->table;
} }
} }
@ -1696,8 +1701,9 @@ class Model extends Overloadable {
} else { } else {
$message = __('This field cannot be left blank',true); $message = __('This field cannot be left blank',true);
} }
$exists = $this->exists();
if (empty($validator['on']) || ($validator['on'] == 'create' && !$this->exists()) || ($validator['on'] == 'update' && $this->exists())) { if (empty($validator['on']) || ($validator['on'] == 'create' && !$exists) || ($validator['on'] == 'update' && $exists)) {
if ((!isset($data[$fieldName]) && $validator['required'] === true) || (isset($data[$fieldName]) && (empty($data[$fieldName]) && !is_numeric($data[$fieldName])) && $validator['allowEmpty'] === false)) { if ((!isset($data[$fieldName]) && $validator['required'] === true) || (isset($data[$fieldName]) && (empty($data[$fieldName]) && !is_numeric($data[$fieldName])) && $validator['allowEmpty'] === false)) {
$this->invalidate($fieldName, $message); $this->invalidate($fieldName, $message);
} elseif (isset($data[$fieldName])) { } elseif (isset($data[$fieldName])) {
@ -1901,7 +1907,6 @@ class Model extends Overloadable {
* @return int * @return int
*/ */
function getNumRows() { function getNumRows() {
//return $this->__numRows;
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db =& ConnectionManager::getDataSource($this->useDbConfig);
return $db->lastNumRows(); return $db->lastNumRows();
} }
@ -1911,7 +1916,6 @@ class Model extends Overloadable {
* @return int * @return int
*/ */
function getAffectedRows() { function getAffectedRows() {
//return $this->__affectedRows;
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db =& ConnectionManager::getDataSource($this->useDbConfig);
return $db->lastAffected(); return $db->lastAffected();
} }