/** * Overrides Table::store to check unique fields. * * @param boolean $updateNulls True to update fields even if they are null. * * @return boolean True on success. * * @since 11.4 */ public function store($updateNulls = false) { // Verify that the sef field is unique $table = Table::getInstance('Language', 'JTable'); if ($table->load(array('sef' => $this->sef)) && ($table->lang_id != $this->lang_id || $this->lang_id == 0)) { $this->setError(Text::_('JLIB_DATABASE_ERROR_LANGUAGE_UNIQUE_SEF')); return false; } // Verify that the image field is unique if ($table->load(array('image' => $this->image)) && ($table->lang_id != $this->lang_id || $this->lang_id == 0)) { $this->setError(Text::_('JLIB_DATABASE_ERROR_LANGUAGE_UNIQUE_IMAGE')); return false; } // Verify that the language code is unique if ($table->load(array('lang_code' => $this->lang_code)) && ($table->lang_id != $this->lang_id || $this->lang_id == 0)) { $this->setError(Text::_('JLIB_DATABASE_ERROR_LANGUAGE_UNIQUE_LANG_CODE')); return false; } return parent::store($updateNulls); }
/** * Method to store a node in the database table. * * @param boolean $updateNulls True to update null values as well. * * @return boolean True on success. * * @link http://docs.joomla.org/JTableNested/store * @since 11.1 */ public function store($updateNulls = false) { $k = $this->_tbl_key; // @codeCoverageIgnoreStart if ($this->_debug) { echo "\n" . get_class($this) . "::store\n"; $this->_logtable(true, false); } // @codeCoverageIgnoreEnd /* * If the primary key is empty, then we assume we are inserting a new node into the * tree. From this point we would need to determine where in the tree to insert it. */ if (empty($this->{$k})) { /* * We are inserting a node somewhere in the tree with a known reference * node. We have to make room for the new node and set the left and right * values before we insert the row. */ if ($this->_location_id >= 0) { // Lock the table for writing. if (!$this->_lock()) { // Error message set in lock method. return false; } // We are inserting a node relative to the last root node. if ($this->_location_id == 0) { // Get the last root node as the reference node. $query = $this->_db->getQuery(true); $query->select($this->_tbl_key . ', parent_id, level, lft, rgt')->from($this->_tbl)->where('parent_id = 0')->order('lft DESC'); $this->_db->setQuery($query, 0, 1); $reference = $this->_db->loadObject(); // @codeCoverageIgnoreStart if ($this->_debug) { $this->_logtable(false); } // @codeCoverageIgnoreEnd } else { // Get the reference node by primary key. if (!($reference = $this->_getNode($this->_location_id))) { // Error message set in getNode method. $this->_unlock(); return false; } } // Get the reposition data for shifting the tree and re-inserting the node. if (!($repositionData = $this->_getTreeRepositionData($reference, 2, $this->_location))) { // Error message set in getNode method. $this->_unlock(); return false; } // Create space in the tree at the new location for the new node in left ids. $query = $this->_db->getQuery(true); $query->update($this->_tbl)->set('lft = lft + 2')->where($repositionData->left_where); $this->_runQuery($query, 'JLIB_DATABASE_ERROR_STORE_FAILED'); // Create space in the tree at the new location for the new node in right ids. $query = $this->_db->getQuery(true); $query->update($this->_tbl)->set('rgt = rgt + 2')->where($repositionData->right_where); $this->_runQuery($query, 'JLIB_DATABASE_ERROR_STORE_FAILED'); // Set the object values. $this->parent_id = $repositionData->new_parent_id; $this->level = $repositionData->new_level; $this->lft = $repositionData->new_lft; $this->rgt = $repositionData->new_rgt; } else { // Negative parent ids are invalid $e = new UnexpectedValueException(sprintf('%s::store() used a negative _location_id', get_class($this))); $this->setError($e); return false; } } else { // If the location has been set, move the node to its new location. if ($this->_location_id > 0) { if (!$this->moveByReference($this->_location_id, $this->_location, $this->{$k})) { // Error message set in move method. return false; } } // Lock the table for writing. if (!$this->_lock()) { // Error message set in lock method. return false; } } // Store the row to the database. if (!parent::store($updateNulls)) { $this->_unlock(); return false; } // @codeCoverageIgnoreStart if ($this->_debug) { $this->_logtable(); } // @codeCoverageIgnoreEnd // Unlock the table for writing. $this->_unlock(); return true; }
/** * Inserts a new row if id is zero or updates an existing row in the database table * * @param boolean $updateNulls If false, null object variables are not updated * * @return boolean True if successful, false otherwise and an internal error message is set * * @since 11.1 */ public function store($updateNulls = false) { if ($result = parent::store($updateNulls)) { // Rebuild the nested set tree. $this->rebuild(); } return $result; }