Index: trunk/core/kernel/db/dbitem.php =================================================================== diff -u -r3081 -r3084 --- trunk/core/kernel/db/dbitem.php (.../dbitem.php) (revision 3081) +++ trunk/core/kernel/db/dbitem.php (.../dbitem.php) (revision 3084) @@ -212,42 +212,54 @@ * * @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) + function GetKeyClause($method=null, $keys_hash = null) { - return '`'.$this->TableName.'`.'.$this->IDField.' = '.$this->Conn->qstr($this->ID); + if( !isset($keys_hash) ) $keys_hash = Array($this->IDField => $this->ID); + + $ret = ''; + foreach($keys_hash as $field => $value) + { + $ret .= '(`'.$this->TableName.'`.'.$field.' = '.$this->Conn->qstr($value).') AND '; + } + + return preg_replace('/(.*) AND $/', '\\1', $ret); } /** * Loads item from the database by given id * * @access public - * @param int $id Primery Key Id to load + * @param mixed $id item id of keys->values hash to load item by * @param string $id_field_name Optional parameter to load item by given Id field * @return bool True if item has been loaded, false otherwise */ - function Load($id, $id_field_name=null) + function Load($id, $id_field_name = null) { - if (is_array($id)) { - $keys = $id; - foreach ($keys as $field => $value) { - $sqls[] = '`'.$this->TableName.'`.'.$field.' = '.$this->Conn->qstr($value); - } - $keys_sql = '('.implode(') AND (', $sqls).')'; + if ( isset($id_field_name) ) $this->SetIDField( $id_field_name ); + $keys_sql = ''; + if( is_array($id) ) + { + $keys_sql = $this->GetKeyClause('load', $id); } + else + { + $this->setID($id); + $keys_sql = $this->GetKeyClause('load'); + } + if ( isset($id_field_name) ) $this->setIDField( $this->Application->getUnitOption($this->Prefix, 'IDField') ); - if (isset($id_field_name)) $this->SetIDField($id_field_name); - if (!isset($id) && !isset($keys_sql)) return false; - if( !$this->raiseEvent('OnBeforeItemLoad',$id) ) return false; + if( ($id === false) || !$keys_sql ) return $this->Clear(); - $this->ID = $id; + if( !$this->raiseEvent('OnBeforeItemLoad', $id) ) return false; - $q = $this->GetSelectSQL().' WHERE '.(isset($keys_sql) ? $keys_sql : $this->GetKeyClause('load')); + $q = $this->GetSelectSQL().' WHERE '.$keys_sql; $field_values = $this->Conn->GetRow($q); if($field_values) @@ -256,22 +268,14 @@ } else { - $this->Loaded = false; - return false; + return $this->Clear(); } - if( isset($keys_sql) ) - { - $this->setID( $this->FieldValues[$this->IDField] ); - } - else - { - $this->setID($id); - } - + if( is_array($id) ) $this->setID( $this->FieldValues[$this->IDField] ); + $this->UpdateFormattersSubFields(); // used for updating separate virtual date/time fields from DB timestamp (for example) - $this->raiseEvent('OnAfterItemLoad'); + $this->raiseEvent('OnAfterItemLoad', $this->GetID() ); $this->Loaded = true; return true; } @@ -764,8 +768,8 @@ */ function setID($new_id) { - $this->ID=$new_id; - $this->SetDBField($this->IDField,$new_id); + $this->ID = $new_id; + $this->SetDBField($this->IDField, $new_id); } /** @@ -851,8 +855,12 @@ function Clear() { + $this->setID(null); + $this->Loaded = false; $this->FieldValues = Array(); + $this->SetDefaultValues(); $this->FieldErrors = Array(); + return $this->Loaded; } }