Friday 28 February 2014

Function to update the DB Table Record in Joomla 3.0+

// Function to update the DB Table Records in Joomla 3.0+ by passing the data in arrays
protected function updateTableRecordByClauses($fields, $table, $where=array())
{
 $this->db = JFactory::getDBO();
 
 $query = "UPDATE ".$table." SET %s WHERE %s";
 
 $fields = array();
 foreach ($fields as $key => $val ) :
  $fields[] = $this->db->quoteName($key) . '=\''.$val.'\'';
 endforeach;
 
 // Conditions for which records should be updated.
 $conditions = array();
 foreach ($where as $key => $val ) :
  $conditions[] = $this->db->quoteName($key) . '=\''.$val.'\'';
 endforeach;
 
 $this->db->setQuery( $query = sprintf($query, implode(",", $fields), implode(" AND ", $conditions)) );
 $result = $this->db->execute();

 return $result;
}
// To call the function 
 $fields = array('Status'=>'Approved');
 $whereClause = array('SubmissionId' => 12 'FieldName' => 'Membership', 'FieldValue' => 'Pending');
 $this->updateTableRecordByClauses($fields, '#__users_submission', $whereClause);


// Function to update the Single Where Clause.
protected function updateTableRecord($locationObj, $table)
{
 $this->db = JFactory::getDBO();
 // Update a new query object.
 $result = $this->db->updateObject($table, $locationObj, 'id');
 // $id = (int)$this->db->insertid();
 
 return $result;
}
 // Update the user record into '__users' table.
 $dataObj = new stdClass();
 $dataObj->id = $myfields['UserId'];
 $dataObj->lastResetTime = $location_id;
 $dataObj->block = $myfields['Country'];
 $dataObj->updated = date('Y-m-d H:i:s');
 $this->updateTableRecord($dataObj, '#__users');

No comments:

Post a Comment

Please post any queries and comments here.