function delete($oid = '') { if (empty($oid)) { // if empty, use the values of the current keys $keynames = $this->getKeyNames(); foreach ($keynames as $key => $value) { $oid[$key] = $this->{$key}; } if (empty($oid)) { // if still empty, fail $this->setError(JText::_("Cannot delete with empty key")); return false; } } if (!is_array($oid)) { $keyName = $this->getKeyName(); $arr = array(); $arr[$keyName] = $oid; $oid = $arr; } $db = $this->getDBO(); // initialize the query $query = new K2StoreQuery(); $query->delete(); $query->from($this->getTableName()); foreach ($oid as $key => $value) { // Check that $key is field in table if (!in_array($key, array_keys($this->getProperties()))) { $this->setError(get_class($this) . ' does not have the field ' . $key); return false; } // add the key=>value pair to the query $value = $db->Quote($db->escape(trim(strtolower($value)))); $query->where($key . ' = ' . $value); } $db->setQuery((string) $query); if ($db->query()) { return true; } else { $this->setError($db->getErrorMsg()); return false; } }
protected function _buildResultQuery() { $query = new K2StoreQuery(); $query->select($this->getState('select', 'COUNT(*)')); $this->_buildQueryFrom($query); $this->_buildQueryJoins($query); $this->_buildQueryWhere($query); $this->_buildQueryGroup($query); $this->_buildQueryHaving($query); return $query; }
/** * Loads a row from the database and binds the fields to the object properties * * @access public * @param mixed Optional primary key. If not specifed, the value of current key is used * @return boolean True if successful */ function load($oid = null, $reset = true) { if (!is_array($oid)) { // load by primary key if not array $keyName = $this->getKeyName(); $oid = array($keyName => $oid); } if (empty($oid)) { // if empty, use the value of the current key $keyName = $this->getKeyName(); $oid = $this->{$keyName}; if (empty($oid)) { // if still empty, fail $this->setError(JText::_("Cannot load with empty key")); return false; } } // allow $oid to be an array of key=>values to use when loading $oid = (array) $oid; if (!empty($reset)) { $this->reset(); } $db = $this->getDBO(); // initialize the query $query = new K2StoreQuery(); $query->select('*'); $query->from($this->getTableName()); foreach ($oid as $key => $value) { // Check that $key is field in table if (!in_array($key, array_keys($this->getProperties()))) { $this->setError(get_class($this) . ' does not have the field ' . $key); return false; } // add the key=>value pair to the query $value = $db->Quote($db->escape(trim(strtolower($value)))); $query->where($key . ' = ' . $value); } $db->setQuery((string) $query); if ($result = $db->loadAssoc()) { $result = $this->bind($result); if ($result) { $dispatcher = JDispatcher::getInstance(); $dispatcher->trigger('onLoad' . $this->get('_suffix'), array(&$this)); } return $result; } else { $this->setError($db->getErrorMsg()); return false; } }