/** * Inserts a new row if id is zero or updates an existing row in the database table * * Can be overloaded/supplemented by the child class * * @access public * @param boolean If false, null object variables are not updated * @return null|string null if successful otherwise returns and error message */ function store($updateNulls = false) { $k = $this->_tbl_key; if ($this->{$k}) { $ret = $this->_db->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls); } else { //loading libraries $properties = $this->getProperties(); //loading resources $user =& eFactory::getUser(); //initializing object properties $this->_juser_id = $user->id; $this->{$k} = create_guid(); if (array_key_exists('created_on', $properties)) { $this->created_on = date('Y-m-d H:i:s', time()); } if ($ret = $this->_db->insertObject($this->_tbl, $this, $this->_tbl_key)) { $this->saveRelationship('juser'); } } if (!$ret) { $this->setError(get_class($this) . '::store failed - ' . $this->_db->getErrorMsg()); return false; } else { return true; } }
/** * Method to add an object to the database. * * @return void * * @since 12.1 * @throws RuntimeException */ protected function doCreate() { // Get the primary key. $primaryKey = $this->getTableKey('primary', 'primary'); try { // Start a transaction. $this->db->transactionStart(); // Store the data for each table. foreach ($this->tables as $alias => $table) { // Get the data for the table. $dump = $this->dumpTable($alias); // Store the data to the database. $this->db->insertObject($table, $dump, $primaryKey); // Update the primary id. if ($alias == 'primary') { // Set the primary key value. $this->{$primaryKey} = (int) $dump->{$primaryKey}; } } // Commit the transaction. $this->db->transactionCommit(); } catch (RuntimeException $error) { // Rollback the transaction. $this->db->transactionRollback(); // Rethrow the error. throw $error; } }
/** * Inserts a new row if id is zero or updates an existing row in the database table * * Can be overloaded/supplemented by the child class * * @access public * @param boolean If false, null object variables are not updated * @return null|string null if successful otherwise returns and error message */ function store($updateNulls = false) { $k = $this->_tbl_key; if ($this->{$k}) { $ret = $this->_db->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls); } else { $ret = $this->_db->insertObject($this->_tbl, $this, $this->_tbl_key); } if (!$ret) { $this->setError(get_class($this) . '::store failed - ' . $this->_db->getErrorMsg()); return false; } else { return true; } }
/** * Method to store a row in the database from the JTable 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 * JTable instance. * * @param boolean $updateNulls True to update fields even if they are null. * * @return boolean True on success. * * @link http://docs.joomla.org/JTable/store * @since 11.1 */ public function store($updateNulls = false) { // Initialise variables. $k = $this->_tbl_key; if (!empty($this->asset_id)) { $currentAssetId = $this->asset_id; } // The asset id field is managed privately by this class. if ($this->_trackAssets) { unset($this->asset_id); } // If a primary key exists update the object, otherwise insert it. if ($this->{$k}) { $stored = $this->_db->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls); } else { $stored = $this->_db->insertObject($this->_tbl, $this, $this->_tbl_key); } // If the store failed return false. if (!$stored) { $e = new JException(JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', get_class($this), $this->_db->getErrorMsg())); $this->setError($e); return false; } // If the table is not set to track assets return true. if (!$this->_trackAssets) { return true; } if ($this->_locked) { $this->_unlock(); } // // Asset Tracking // $parentId = $this->_getAssetParentId(); $name = $this->_getAssetName(); $title = $this->_getAssetTitle(); $asset = JTable::getInstance('Asset', 'JTable', array('dbo' => $this->getDbo())); $asset->loadByName($name); // Re-inject the asset id. $this->asset_id = $asset->id; // Check for an error. if ($error = $asset->getError()) { $this->setError($error); return false; } // Specify how a new or moved node asset is inserted into the tree. if (empty($this->asset_id) || $asset->parent_id != $parentId) { $asset->setLocation($parentId, 'last-child'); } // Prepare the asset to be stored. $asset->parent_id = $parentId; $asset->name = $name; $asset->title = $title; if ($this->_rules instanceof JAccessRules) { $asset->rules = (string) $this->_rules; } if (!$asset->check() || !$asset->store($updateNulls)) { $this->setError($asset->getError()); return false; } // Create an asset_id or heal one that is corrupted. if (empty($this->asset_id) || $currentAssetId != $this->asset_id && !empty($this->asset_id)) { // Update the asset_id field in this table. $this->asset_id = (int) $asset->id; $query = $this->_db->getQuery(true); $query->update($this->_db->quoteName($this->_tbl)); $query->set('asset_id = ' . (int) $this->asset_id); $query->where($this->_db->quoteName($k) . ' = ' . (int) $this->{$k}); $this->_db->setQuery($query); if (!$this->_db->execute()) { $e = new JException(JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED_UPDATE_ASSET_ID', $this->_db->getErrorMsg())); $this->setError($e); return false; } } return true; }
/** * Insert an object into database * * @param string $table This is expected to be a valid (and safe!) table name * @param object &$object A reference to an object whose public properties match the table fields. * @param string $keyName The name of the primary key. If provided the object property is updated. * @return boolean TRUE if insert succeeded, FALSE when error * * @throws \RuntimeException */ public function insertObject($table, &$object, $keyName = null) { return $this->_db->insertObject($table, $object, $keyName); }
/** * Insert an object into a table * * @param AppTable $table The table object in which to insert the object * @param object $object The object to insert * @param string $key The name of the primary key of the table * * @return boolean If the operation was successful * * @throws RuntimeException * * @since 1.0.0 */ public function insertObject($table, $object, $key = null) { // insert object return $this->_database->insertObject($table, $object, $key); }
/** * Insert an object into database * * @param string $table This is expected to be a valid (and safe!) table name * @param object $object * @param string $keyName * @param boolean $verbose * @return boolean TRUE if insert succeeded, FALSE when error */ function insertObject($table, &$object, $keyName = NULL, $verbose = false) { return $this->_db->insertObject($table, $object, $keyName, $verbose); }
/** * Inserts a new row if id is zero or updates an existing row in the database table * * Can be overloaded/supplemented by the child class * * @access public * @param boolean If false, null object variables are not updated * @return null|string null if successful otherwise returns and error message */ function store($updateNulls = true) { $k = $this->_tbl_key; $wheres = array(); // execute query and retrieve result $query = $this->_db->getQuery(true); $query->select(' 1 '); $query->from(' ' . $this->_db->quoteName($this->_tbl) . ' '); // check whether all fields are filled and build where statement for ($i = 0; $i < count($k); $i++) { if ($this->{$k}[$i] === null) { $this->setError(get_class($this) . '::store failed - primary key ' . $this->{$k} . ' is empty (null)'); return false; } else { $wheres[] = ' ' . $this->_db->quoteName($k[$i]) . ' = ' . $this->_db->Quote($this->{$k}[$i]) . ' '; } } foreach ($wheres as $where) { $query->where($where); } $this->_db->setQuery($query); $result = $this->_db->loadResult(); // if query has no result, record does not exists yet and is added here // if query has result, record exists and is updated here if (!$result) { $ret = $this->_db->insertObject($this->_tbl, $this); } else { $query->clear(); $query->update($this->_db->quoteName($this->_tbl)); $sets = array(); foreach ($this->getProperties() as $name => $value) { if (is_array($value) or is_object($value) or $name[0] == '_') { // internal or NA field continue; } if (in_array($name, $k)) { // PK not to be updated continue; } if ($value === null) { if ($updateNulls) { $val = 'NULL'; } else { continue; } } else { //$val = $this->_db->isQuoted( $name ) ? $this->_db->Quote( $value ) : (int) $value; $val = $this->_db->Quote($value); } $sets[] = $this->_db->quoteName($name) . '=' . $val; } if (count($sets) > 0) { foreach ($sets as $set) { $query->set($set); } foreach ($wheres as $where) { $query->where($where); } $this->_db->setQuery($query); $ret = $this->_db->query(); } else { $ret = true; } } if (!$ret) { $this->setError(get_class($this) . '::store failed - ' . $this->_db->getErrorMsg()); return false; } else { return true; } }