Ejemplo n.º 1
0
 /**
  * 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()
 {
     $k = $this->_tbl_key;
     $where_pk = array();
     $this->reset();
     $query = $this->_db->getQuery(true);
     $query->select(' * ');
     $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) {
             return false;
         } else {
             $query->where(' ' . $this->_db->quoteName($k[$i]) . ' = ' . $this->_db->Quote($this->{$k}[$i]) . ' ');
         }
     }
     $this->_db->setQuery($query);
     if ($result = $this->_db->loadAssoc()) {
         return $this->bind($result);
     } else {
         if ($error = $this->_db->getErrorMsg()) {
             throw new JException($error);
         }
         return false;
     }
 }
Ejemplo n.º 2
0
 /**
  * Method to load a row from the database by primary key and bind the fields
  * to the JTable instance properties.
  *
  * @param   mixed    $keys   An optional primary key value to load the row by, or an array of fields to match.  If not
  * set the instance property value is used.
  * @param   boolean  $reset  True to reset the default values before loading the new row.
  *
  * @return  boolean  True if successful. False if row not found or on error (internal error state set in that case).
  *
  * @link    http://docs.joomla.org/JTable/load
  * @since   11.1
  */
 public function load($keys = null, $reset = true)
 {
     if (empty($keys)) {
         // If empty, use the value of the current key
         $keyName = $this->_tbl_key;
         $keyValue = $this->{$keyName};
         // If empty primary key there's is no need to load anything
         if (empty($keyValue)) {
             return true;
         }
         $keys = array($keyName => $keyValue);
     } elseif (!is_array($keys)) {
         // Load by primary key.
         $keys = array($this->_tbl_key => $keys);
     }
     if ($reset) {
         $this->reset();
     }
     // Initialise the query.
     $query = $this->_db->getQuery(true);
     $query->select('*');
     $query->from($this->_tbl);
     $fields = array_keys($this->getProperties());
     foreach ($keys as $field => $value) {
         // Check that $field is in the table.
         if (!in_array($field, $fields)) {
             $e = new JException(JText::sprintf('JLIB_DATABASE_ERROR_CLASS_IS_MISSING_FIELD', get_class($this), $field));
             $this->setError($e);
             return false;
         }
         // Add the search tuple to the query.
         $query->where($this->_db->quoteName($field) . ' = ' . $this->_db->quote($value));
     }
     $this->_db->setQuery($query);
     try {
         $row = $this->_db->loadAssoc();
     } catch (RuntimeException $e) {
         $je = new JException($e->getMessage());
         $this->setError($je);
         return false;
     }
     // Legacy error handling switch based on the JError::$legacy switch.
     // @deprecated  12.1
     if (JError::$legacy && $this->_db->getErrorNum()) {
         $e = new JException($this->_db->getErrorMsg());
         $this->setError($e);
         return false;
     }
     // Check that we have a result.
     if (empty($row)) {
         $e = new JException(JText::_('JLIB_DATABASE_ERROR_EMPTY_ROW_RETURNED'));
         $this->setError($e);
         return false;
     }
     // Bind the object with the row and return.
     return $this->bind($row);
 }
Ejemplo n.º 3
0
 /**
  * This global function loads the first row of a query into an object
  *
  * If an object is passed to this function, the returned row is bound to the existing elements of <var>object</var>.
  * If <var>object</var> has a value of null, then all of the returned query fields returned in the object.
  * @param  object|\stdClass  $object
  * @return boolean          Success
  *
  * @throws  \RuntimeException
  */
 public function loadObject(&$object)
 {
     if ($object === null) {
         $object = $this->_db->loadObject();
         return is_object($object);
     }
     $array = $this->_db->loadAssoc();
     if (!is_array($array)) {
         return false;
     }
     foreach (get_object_vars($object) as $k => $v) {
         if (substr($k, 0, 1) != '_') {
             if (array_key_exists($k, $array)) {
                 $object->{$k} = $array[$k];
             }
         }
     }
     return true;
 }
Ejemplo n.º 4
0
 /**
  * Method builds this model based on a contact's id
  * 
  * @param integer $user_id
  * @return boolean
  */
 function load($id = null)
 {
     //reasons to fail
     if (is_null($id) || !$id) {
         return false;
     }
     if (!is_string($id)) {
         return false;
     }
     //initializing variables
     $id = addslashes($id);
     //Get the Contacts information
     $query = "SELECT * " . " FROM `" . $this->_tbl . "`" . " WHERE `id` = '{$id}'";
     $this->_db->setQuery($query);
     $result = $this->_db->loadAssoc();
     if (!$result) {
         return false;
     }
     $this->bind($result);
     //loading any custom properties
     $this->loadCustomProperties();
     return true;
 }
Ejemplo n.º 5
0
 /**
  * Execute a query to the database and get the first row of the result set from the database query as an associative array
  *
  * The result returned by this method is the same as the one returned by JDatabase::loadAssoc()
  * Generally used for SELECT operations
  *
  * @param string $query The query to be executed
  *
  * @return array The first row of the result of the query
  *
  * @throws RuntimeException
  *
  * @since 1.0.0
  */
 public function queryAssoc($query)
 {
     // query database table
     $this->_database->setQuery($query);
     return $this->_database->loadAssoc();
 }
 /**
  * Fetch a result row as an associative array
  *
  * @return array
  */
 function loadAssoc()
 {
     if (is_callable(array($this->_db, 'loadAssoc'))) {
         return $this->_db->loadAssoc();
     } else {
         // new independant efficient implementation:
         if (!($cur = $this->query())) {
             $result = null;
         } else {
             $result = $this->m_fetch_assoc($cur);
             if (!$result) {
                 $result = null;
             }
             $this->m_free_result($cur);
         }
         return $result;
     }
 }