Index: branches/RC/core/units/general/cat_dbitem.php =================================================================== diff -u -r8929 -r9021 --- branches/RC/core/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 8929) +++ branches/RC/core/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 9021) @@ -65,19 +65,17 @@ $ret = parent::Create(); if ($ret) { - $primary_category = $this->GetDBField('CategoryId') > 0 ? $this->GetDBField('CategoryId') : $this->Application->GetVar('m_cat_id'); - $fields_hash = Array( - 'CategoryId' => $primary_category, - 'ItemResourceId' => $this->GetField('ResourceId'), - 'PrimaryCat' => 1, - 'ItemPrefix' => $this->Prefix, - 'Filename' => $this->GetDBField('Filename'), - ); - $this->Conn->doInsert($fields_hash, $this->CategoryItemsTable()); + $this->assignPrimaryCategory(); } return $ret; } + function assignPrimaryCategory() + { + $primary_category = $this->GetDBField('CategoryId') > 0 ? $this->GetDBField('CategoryId') : $this->Application->GetVar('m_cat_id'); + $this->assignToCategory($primary_category, true); + } + function Update($id=null, $system_update=false) { $this->VirtualFields['ResourceId'] = Array(); @@ -319,7 +317,7 @@ */ function assignToCategory($category_id, $is_primary = false) { - $table = $this->mode == 't' ? $this->Application->GetTempName(TABLE_PREFIX.'CategoryItems', 'prefix:'.$this->Prefix) : TABLE_PREFIX.'CategoryItems'; + $table = $this->CategoryItemsTable(); $key_clause = '(ItemResourceId = '.$this->GetDBField('ResourceId').')'; // get all cateories, where item is in @@ -357,9 +355,14 @@ $this->Conn->Query($sql); } else { - $sql = 'INSERT INTO '.$table.' (CategoryId,ItemResourceId,PrimaryCat,ItemPrefix,Filename) VALUES (%s,%s,%s,%s,%s)'; - $filename = $this->useFilenames ? $this->GetDBField('Filename') : ''; // because some prefixes does not use filenames - $this->Conn->Query( sprintf($sql, $category_id, $this->GetDBField('ResourceId'), $is_primary ? 1 : 0, $this->Conn->qstr($this->Prefix), $this->Conn->qstr($filename)) ); + $fields_hash = Array( + 'CategoryId' => $category_id, + 'ItemResourceId' => $this->GetField('ResourceId'), + 'PrimaryCat' => $is_primary ? 1 : 0, + 'ItemPrefix' => $this->Prefix, + 'Filename' => $this->useFilenames ? $this->GetDBField('Filename') : '', // because some prefixes does not use filenames, + ); + $this->Conn->doInsert($fields_hash, $table); } // to ensure filename update after adding to another category // this is critical since there may be an item with same filename in newly added category! @@ -499,6 +502,33 @@ } } + /** + * Returns part of SQL WHERE clause identifing the record, ex. id = 25 + * + * @access public + * @param string $method Child class may want to know who called GetKeyClause, Load(), Update(), Delete() send its names as method + * @param Array $keys_hash alternative, then item id, keys hash to load item by + * @return void + * @see kDBItem::Load() + * @see kDBItem::Update() + * @see kDBItem::Delete() + */ + function GetKeyClause($method = null, $keys_hash = null) + { + if ($method == 'load') { + // for item with many categories makes primary to load + $ci_table = TABLE_PREFIX.'CategoryItems'; + if ($this->IsTempTable()) { + $ci_table = $this->Application->GetTempName($ci_table, 'prefix:'.$this->Prefix); + } + $keys_hash = Array( + $this->IDField => $this->ID, + '`'.$ci_table.'`.`PrimaryCat`' => 1, + ); + } + return parent::GetKeyClause($method, $keys_hash); + } + } ?> \ No newline at end of file