Merging fixes and enhancements into trunk.

Revision: [2031]
Added ability to turn off the cascading deletes when using Model::del($id, false);

Revision: [2030]
Corrected association cascading for hasOne and hasMany.

Revision: [2029]
Implemented cascading delete for hasMany, and hasOne associations.

Revision: [2028]
Renamed Model::__deleteJoins() to Model::__deleteMulti();

Revision: [2027]
Implemented Model::__deleteJoins() to delete HABTM associations.

git-svn-id: https://svn.cakephp.org/repo/trunk/cake@2032 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2006-02-19 01:54:39 +00:00
parent a75e08976f
commit a6051579d7
3 changed files with 91 additions and 32 deletions

View file

@ -6,4 +6,4 @@
// +---------------------------------------------------------------------------------------------------+ //
///////////////////////////////////////////////////////////////////////////////////////////////////////////
0.10.8.2026
0.10.8.2032

View file

@ -436,7 +436,7 @@ class Model extends Object
$colKey = Inflector::underscore($className);
if(ClassRegistry::isKeySet($colKey))
{
$this->{$className} =& ClassRegistry::getObject($colKey);
$this->{$className} =& ClassRegistry::getObject($colKey);
}
else
{
@ -953,22 +953,20 @@ class Model extends Object
* @param mixed $id Id of record to delete
* @return boolean True on success
*/
function del ($id = null, $cascade = false)
function del ($id = null, $cascade = true)
{
if ($id)
{
$this->id = $id;
}
$id = $this->id;
if($this->beforeDelete())
{
if ($this->id && $this->db->delete($this))
{
//$this->__deleteJoins($id);
if ($cascade)
{
//$this->__deleteMulti($id);
}
$this->__deleteMulti($id);
$this->__deleteHasMany($id, $cascade);
$this->__deleteHasOne($id, $cascade);
$this->afterDelete();
$this->id = false;
return true;
@ -985,17 +983,47 @@ class Model extends Object
* @return null
* @access private
*/
function __deleteMulti ($id)
function __deleteHasMany ($id, $cascade)
{
foreach ($this->hasMany as $assoc => $data)
{
$model =& $this->{$data['className']};
$field = $model->escapeField($data['foreignKey']);
$records = $model->findAll($field.'='.$id);
foreach($records as $record)
if($data['dependent'] === true && $cascade === true)
{
$model =& $this->{$data['className']};
$field = $model->escapeField($data['foreignKey']);
$model->recursive = 0;
$records = $model->findAll($field.'='.$id);
foreach($records as $record)
{
$this->{$data['className']}->del($record[$data['className']][$model->{$data['className']}->primaryKey]);
}
}
}
}
/**
* Cascades model deletes to hasOne relationships.
*
* @param string $id
* @return null
* @access private
*/
function __deleteHasOne ($id, $cascade)
{
foreach ($this->hasOne as $assoc => $data)
{
if($data['dependent'] === true && $cascade === true)
{
$model =& $this->{$data['className']};
$field = $model->escapeField($data['foreignKey']);
$model->recursive = 0;
$records = $model->findAll($field.'='.$id);
foreach($records as $record)
{
$this->{$data['className']}->del($record[$data['className']][$model->primaryKey]);
}
}
}
}
@ -1007,13 +1035,15 @@ class Model extends Object
* @return null
* @access private
*/
function __deleteJoins ($id)
function __deleteMulti ($id)
{
foreach ($this->hasAndBelongsToMany as $assoc => $data)
{
$this->db->execute("DELETE FROM {$this->db->name($data['joinTable'])} WHERE {$this->db->name($data['foreignKey'])} = '{$id}'");
}
}
/**
* Returns true if a record with set id exists.
*

View file

@ -949,22 +949,20 @@ class Model extends Object
* @param mixed $id Id of record to delete
* @return boolean True on success
*/
function del ($id = null, $cascade = false)
function del ($id = null, $cascade = true)
{
if ($id)
{
$this->id = $id;
}
$id = $this->id;
if($this->beforeDelete())
{
if ($this->id && $this->db->delete($this))
{
//$this->__deleteJoins($id);
if ($cascade)
{
//$this->__deleteMulti($id);
}
$this->__deleteMulti($id);
$this->__deleteHasMany($id, $cascade);
$this->__deleteHasOne($id, $cascade);
$this->afterDelete();
$this->id = false;
return true;
@ -981,17 +979,47 @@ class Model extends Object
* @return null
* @access private
*/
function __deleteMulti ($id)
function __deleteHasMany ($id, $cascade)
{
foreach ($this->hasMany as $assoc => $data)
{
$model =& $this->{$data['className']};
$field = $model->escapeField($data['foreignKey']);
$records = $model->findAll($field.'='.$id);
foreach($records as $record)
if($data['dependent'] === true && $cascade === true)
{
$model =& $this->{$data['className']};
$field = $model->escapeField($data['foreignKey']);
$model->recursive = 0;
$records = $model->findAll($field.'='.$id);
foreach($records as $record)
{
$this->{$data['className']}->del($record[$data['className']][$model->{$data['className']}->primaryKey]);
}
}
}
}
/**
* Cascades model deletes to hasOne relationships.
*
* @param string $id
* @return null
* @access private
*/
function __deleteHasOne ($id, $cascade)
{
foreach ($this->hasOne as $assoc => $data)
{
if($data['dependent'] === true && $cascade === true)
{
$model =& $this->{$data['className']};
$field = $model->escapeField($data['foreignKey']);
$model->recursive = 0;
$records = $model->findAll($field.'='.$id);
foreach($records as $record)
{
$this->{$data['className']}->del($record[$data['className']][$model->primaryKey]);
}
}
}
}
@ -1003,10 +1031,11 @@ class Model extends Object
* @return null
* @access private
*/
function __deleteJoins ($id)
function __deleteMulti ($id)
{
foreach ($this->hasAndBelongsToMany as $assoc => $data)
{
$this->db->execute("DELETE FROM {$this->db->name($data['joinTable'])} WHERE {$this->db->name($data['foreignKey'])} = '{$id}'");
}
}