/** * Method to store a row in the database from the FOFTable instance properties. * If a primary key value is set the row with that primary key value will be * updated with the instance property values. If no primary key value is set * a new row will be inserted into the database with the properties from the * FOFTable instance. * * @param boolean $updateNulls True to update fields even if they are null. * * @return boolean True on success. */ public function store($updateNulls = false) { if (!$this->onBeforeStore($updateNulls)) { return false; } $k = $this->_tbl_key; if ($this->{$k} == 0) { $this->{$k} = null; } // Create the object used for inserting/updating data to the database $fields = $this->getTableFields(); $properties = $this->getKnownFields(); $keys = array(); foreach ($properties as $property) { // 'input' property is a reserved name if (isset($fields[$property])) { $keys[] = $property; } } $updateObject = array(); foreach ($keys as $key) { $updateObject[$key] = $this->{$key}; } $updateObject = (object) $updateObject; /** * While the documentation for update/insertObject and execute() say they return a boolean, * not all of the implemtnations. Depending on the version of J! and the specific driver, * they may return a database object, or boolean, or a mix, or toss an exception. So try/catch, * and test for false. */ try { // If a primary key exists update the object, otherwise insert it. if ($this->{$k}) { $result = $this->_db->updateObject($this->_tbl, $updateObject, $this->_tbl_key, $updateNulls); } else { $result = $this->_db->insertObject($this->_tbl, $updateObject, $this->_tbl_key); } if ($result === false) { $this->setError($this->_db->getErrorMsg()); return false; } } catch (Exception $e) { $this->setError($e->getMessage()); } $this->bind($updateObject); if ($this->_locked) { $this->_unlock(); } $result = $this->onAfterStore(); return $result; }