/** * Initialise a new record and return id. * * @param int|string $id The id to find. * @param mixed $row The added row. * * @throws \InvalidArgumentException * @return bool|string|int Return init id. */ public function initRow($id, $row = array()) { if ($this->exists($id)) { return $id; } $row = $row ? (object) $row : new \stdClass(); $row->{$this->pkName} = $id; if (!$this->db->insertObject($this->table, $row, $this->pkName)) { return false; } return $row->{$this->pkName}; }
public function store($updateNulls = false) { if ($this->exists()) { $ret = static::$db->updateObject(static::$tbl, $this, static::$tbl_keys, $updateNulls); } else { $ret = static::$db->insertObject(static::$tbl, $this, static::$tbl_keys); } if (static::$locked) { $this->unlock(); } if (!$ret) { $this->setError(get_class($this) . '::store failed - ' . static::$db->getErrorMsg()); return false; } $this->_exists = true; return true; }
/** * Do create action. * * @param mixed $dataset The data set contains data we want to store. * * @throws \Exception * @return mixed Data set data with inserted id. */ protected function doCreate($dataset) { $this->db->transactionStart(true); try { foreach ($dataset as &$data) { $entity = new Entity($this->getFields($this->table), $data); $pk = $this->getPrimaryKey(); $this->db->insertObject($this->table, $entity, $pk); $data->{$pk} = $entity->{$pk}; } } catch (\Exception $e) { $this->db->transactionRollback(true); throw $e; } $this->db->transactionCommit(true); return $dataset; }
/** * Insert new row * * @param array $attributes * * @return bool|mixed|void */ public function insert(array $attributes) { $object = new stdClass(); foreach ($attributes as $key => $value) { $object->{$key} = $value; } // Insert the object into the user profile table. try { return $this->db->insertObject($this->tableName, $object); } catch (Exception $e) { return false; } }
/** * 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; // 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 !== true) { $this->setError($this->_db->getErrorMsg()); return false; } $this->bind($updateObject); if ($this->_locked) { $this->_unlock(); } $result = $this->onAfterStore(); return $result; }
/** * 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}) { $this->_db->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls); } else { $this->_db->insertObject($this->_tbl, $this, $this->_tbl_key); } // 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 = self::getInstance('Asset', 'JTable', array('dbo' => $this->getDbo())); $asset->loadByName($name); // Re-inject the asset id. $this->asset_id = $asset->id; // Check for an error. $error = $asset->getError(); if ($error) { $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); $this->_db->execute(); } 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 https://docs.joomla.org/JTable/store * @since 11.1 */ public function store($updateNulls = false) { $result = true; $k = $this->_tbl_keys; // Implement JObservableInterface: Pre-processing by observers $this->_observers->update('onBeforeStore', array($updateNulls, $k)); $currentAssetId = 0; 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->hasPrimaryKey()) { $this->_db->updateObject($this->_tbl, $this, $this->_tbl_keys, $updateNulls); } else { $this->_db->insertObject($this->_tbl, $this, $this->_tbl_keys[0]); } // If the table is not set to track assets return true. if ($this->_trackAssets) { if ($this->_locked) { $this->_unlock(); } /* * Asset Tracking */ $parentId = $this->_getAssetParentId(); $name = $this->_getAssetName(); $title = $this->_getAssetTitle(); $asset = self::getInstance('Asset', 'JTable', array('dbo' => $this->getDbo())); $asset->loadByName($name); // Re-inject the asset id. $this->asset_id = $asset->id; // Check for an error. $error = $asset->getError(); if ($error) { $this->setError($error); return false; } else { // 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; } else { // 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)->update($this->_db->quoteName($this->_tbl))->set('asset_id = ' . (int) $this->asset_id); $this->appendPrimaryKeys($query); $this->_db->setQuery($query)->execute(); } } } } // Implement JObservableInterface: Post-processing by observers $this->_observers->update('onAfterStore', array(&$result)); return $result; }
/** * 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); }
/** * 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. * * @since 11.1 */ public function store($updateNulls = false) { $result = true; $k = $this->_tbl_keys; // Pre-processing by observers $event = AbstractEvent::create('onTableBeforeStore', ['subject' => $this, 'updateNulls' => $updateNulls, 'k' => $k]); $this->getDispatcher()->dispatch('onTableBeforeStore', $event); $currentAssetId = 0; 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); } // We have to unset typeAlias since updateObject / insertObject will try to insert / update all public variables... $typeAlias = $this->typeAlias; unset($this->typeAlias); try { // If a primary key exists update the object, otherwise insert it. if ($this->hasPrimaryKey()) { $this->_db->updateObject($this->_tbl, $this, $this->_tbl_keys, $updateNulls); } else { $this->_db->insertObject($this->_tbl, $this, $this->_tbl_keys[0]); } } catch (\Exception $e) { $this->setError($e->getMessage()); $result = false; } $this->typeAlias = $typeAlias; // If the table is not set to track assets return true. if ($this->_trackAssets) { if ($this->_locked) { $this->_unlock(); } /* * Asset Tracking */ $parentId = $this->_getAssetParentId(); $name = $this->_getAssetName(); $title = $this->_getAssetTitle(); $asset = self::getInstance('Asset', 'JTable', array('dbo' => $this->getDbo())); $asset->loadByName($name); // Re-inject the asset id. $this->asset_id = $asset->id; // Check for an error. $error = $asset->getError(); if ($error) { $this->setError($error); return false; } else { // 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; } else { // 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)->update($this->_db->quoteName($this->_tbl))->set('asset_id = ' . (int) $this->asset_id); $this->appendPrimaryKeys($query); $this->_db->setQuery($query)->execute(); } } } } // Post-processing by observers $event = AbstractEvent::create('onTableAfterStore', ['subject' => $this, 'result' => &$result]); $this->getDispatcher()->dispatch('onTableAfterStore', $event); return $result; }
/** * Do create action. * * @param string $table The table name. * @param mixed $data The data set contains data we want to store. * @param string $pk The primary key column name. * * @return mixed Data set data with inserted id. */ public function create($table, $data, $pk = null) { return $this->db->insertObject($table, $data, $pk); }
/** * 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); }