To be a tad more helpful, for more hints search http://groups.google.com/group/cake-php/ for "HABTM".
Nate states it quite clearly in the thread named "Additional information with a HABTM?" And in "Database Association Class (HABTM) with Additional Columns" it says: 'First of all, Cake does not currently support multiple primary keys...' To facilitate primary keys consisting of more than just one 'id' field: https://trac.cakephp.org/ticket/1237 "Allow (SQL) DELETE for join tables and other multi-column primary keys" Hmmm I seem to have forgotten to submit the INSET counterpart of that patch to TraC. But see below for both patches/diffs. ========== allow_multi_column_key_DELETE.diff: --- cake_1.1.6.3264/cake/libs/model/datasources/dbo_source.php 2006-07-15 02:34:58.000000000 +0200 +++ new/cake/libs/model/datasources/dbo_source.php 2006-07-28 16:57:48.477119752 +0200 @@ -1050,8 +1104,25 @@ $model->id = array($model->id); } + foreach($model->id as $id) { - $result = $this->execute('DELETE FROM ' . $this->fullTableName($model) . ' WHERE ' . $this->name($model->primaryKey) . ' = ' . $this->value($id)); + //!!FIX AJS + // allow DELETEs in tables with multi-column primary keys + // In the model define var $primaryKey = array('col1','col2') + // then pass id as array(array(col1, col2)) to delete() + $pkey = $this->name($model->primaryKey); + $where = NULL; + if (is_array($pkey)) { + if (is_array($id)) { + reset($pkey); reset($id); + while (list($k,$v) = each($pkey)) { + if (isset($where)) $where.=' AND '; + $where.=($v.' = '.$this->value(current($id))); + next($id); + } + } else return false; + } else $where = $pkey.' = '.$this->value($id); + $result = $this->execute('DELETE FROM ' . $this->fullTableName($model) . ' WHERE ' . $where); } if ($result) { ========== allow_multi_column_key_INSERT.diff: --- cake/libs/model/model_php5.php 2006-07-14 17:10:55.000000000 +0200 +++ new/cake/libs/model/model_php5.php 2006-07-31 14:00:25.738600860 +0200 @@ -852,19 +852,22 @@ } } else { if ($db->create($this, $fields, $values)) { - $this->__insertID = $db->lastInsertId($this->tablePrefix . $this->table, $this->primaryKey); - - if (!$this->__insertID && $newID != null) { - $this->__insertID = $newID; - $this->id = $newID; + if (is_array($this->primaryKey)) { + $this->id = $values; } else { - $this->id = $this->__insertID; - } + $this->__insertID = $db->lastInsertId($this->tablePrefix . $this->table, $this->primaryKey); - if (!empty($joined)) { - $this->__saveMulti($joined, $this->id); - } + if (!$this->__insertID && $newID != null) { + $this->__insertID = $newID; + $this->id = $newID; + } else { + $this->id = $this->__insertID; + } + if (!empty($joined)) { + $this->__saveMulti($joined, $this->id); + } + } $this->afterSave(); $this->data = false; $this->_clearCache(); @@ -1044,12 +1047,26 @@ if ($this->id) { $id = $this->id; - if (is_array($id)) { - $id = $id[0]; - } $db =& ConnectionManager::getDataSource($this->useDbConfig); - return $db->hasAny($this, array($this->primaryKey => $id)); + + if (is_array($this->primaryKey)) { // !!AJS FIX + if (!is_array($id)) return false; + if (count($this->primaryKey) != count($id)) return false; + $condition = array(); + reset($id); + foreach($this->primaryKey as &$columnName) { + $condition[$columnName] = current($id); + next($id); + } + reset($id); + } + else { + if (is_array($id)) $id = $id[0]; + $condition = array($this->primaryKey => $id); + } + + return $db->hasAny($this, $condition); } return false; } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~----------~----~----~----~------~----~------~--~---
