_dba = &$dba; $this->_def = &$def; $this->_finder = &$finder; $this->_model = &$model; $this->_current = $current; $this->_saved = !empty($current); $this->_validator = &new FAValidator; $this->_observer = &new FARecordValidatorObserver; $this->_validator->addObserver($this->_observer); $this->_def->setBaseCriteria($this->_validator); } function delete() { if ($this->_saved) { $ret = FALSE; $where = $this->_def->getPrimaryKey()."=?"; $delete = "DELETE FROM " . $this->_def->getDeleteTables(); $where = " WHERE " . $where . $this->_def->getDeleteWhere(); $sql = $delete.$where; $this->_dba->executeUpdate($sql, $this->getId()); $ret = (bool)($this->_dba->affectedRows() > 0); if ($ret) { $this->_current = array(); $this->_dirty = array(); } } } function &get($key) { $ret = NULL; $field = $key; if ($field) { $method = 'get' . camelize($key); // Make sure that a method of this class doesn't get called (cyclic calls to get, etc) // !method_exists(__CLASS__, $method) && // TODO: // PHP4 gets the class name as lowercase, which means it won't work. The above will return // true in php5 because php5 does stuff right. if (method_exists($this, $method)) { eval("\$ret = &\$this->$method();"); } else { $ret = $this->_get($field); } } return $ret; } function getArray() { return $this->_current; } function &getFinder($table = '') { if ($table == '') { $ret = &$this->_finder; } else { $ret = &$this->_model->getFinder($table); } return $ret; } function getInvalid() { return $this->_observer->getInvalid(); } function getId() { $ret = NULL; $pk = $this->_def->getPrimaryKey(); if (isset($this->_current[$pk])) { $ret = $this->_current[$pk]; } if($ret === NULL) { if($this->_inserted !== NULL) { $ret = $this->_dba->getInsertId(); } } return $ret; } // Whether the record has been modified function isDirty() { return !empty($this->_dirty); } // Whether there is a stored copy of the record (current or not) function isSaved() { return $this->_saved; } function isValid($field) { $this->validate(); $invalid = $this->getInvalid(); return in_array($field, $invalid); } // If the record was stored in a session, revive it function revive(&$dba, &$finder, &$model) { $this->_dba = &$dba; $this->_finder = &$finder; $this->_model = &$model; } function save() { $ret = FALSE; if ($this->isDirty() && $this->validate()) { if ($this->isSaved()) { $this->_update(); } else { $this->_insert(); if($this->_def->getPrimaryKey()) { $this->set($this->_def->getPrimaryKey(), $this->_dba->getInsertId()); } } $ret = TRUE; // TODO: Should I put this back? //$ret = (bool)($this->_dba->affectedRows() > 0); if ($this->_dba->affectedRows() > 1) { trigger_error("An update on a single record modified more than one row", E_USER_WARNING); } if ($ret) { $this->_current = array_merge($this->_current, $this->_dirty); $this->_dirty = array(); $this->_saved = TRUE; } } return $ret; } function set($key, $value) { $method = 'set' . camelize($key); // Make sure that a method of this class doesn't get called (cyclic calls to get, etc) if (!method_exists(__CLASS__, $method) && method_exists($this, $method)) { $this->$method($key, $value); // peter: added in. } else { $field = $key; $this->_dirty[$field] = $value; } return TRUE; } function validate() { $this->_validator->run($this); return $this->_observer->isValid(); } function _get($key) { $ret = ''; $field = $key; if (isset($this->_dirty[$field])) { $ret = $this->_dirty[$field]; } elseif (isset($this->_current[$field])) { $ret = $this->_current[$field]; } return $ret; } function _insert() { $fields = ''; $values = ''; $sep = ''; foreach ($this->_dirty as $key => $value) { $fields .= $sep . $this->_def->getField($key, TRUE); // added a boolean thing to use prefix or not $values .= $sep . '?'; $sep = ','; } $insert = "INSERT INTO " . $this->_def->getUpdateTables(); $fields = " ($fields)"; $values = " VALUES ($values)"; $sql = $insert.$fields.$values; $this->_dba->executeUpdate($sql, array_values($this->_dirty)); if($this->_dba->getInsertId()) { $this->_inserted = TRUE; $this->_current[$this->_def->getPrimaryKey()] = $this->_dba->getInsertId(); } } function _update() { $pairs = $this->_dirty; $pk = $this->_def->getPrimaryKey(); if ($pairs[$pk] == $this->getId()) { unset($pairs[$pk]); } $set = ''; $sep = ''; foreach ($pairs as $field => $value) { $set .= $sep . $field . "=?"; $sep = ','; } $where = "$pk=?"; $pairs[] = $this->getId(); $update = "UPDATE " . $this->_def->getUpdateTables(); $set = " SET " . $set; $where = " WHERE " . $where . $this->_def->getUpdateWhere(); $sql = $update.$set.$where; $this->_dba->executeUpdate($sql, array_values($pairs)); } } ?>