How to retrieve data from another table while retrieving data from a table in loadFormData function in Joomla 2.5
I have to show data from two different tables.
Therefore I had to change 'loadFormData' function to satisfy that.
Current implementation was as follows.
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
* @since 1.6
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_speakee.edit.batchofcard.data', array());
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
Then I changed it to get data from another table called 'Discount' by calling table of 'Discount' and got table properties by primary key.
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
* @since 1.6
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_speakee.edit.batchofcard.data', array());
if (empty($data))
{
$data = $this->getItem();
//Get discount table
$table_discount = $this->getTable("Discount");
$discount_data = $table_discount->load($data->discount_id);
// Check for a table object error.
if ($discount_data === false && $table_discount->getError())
{
$this->setError($table_discount->getError());
return false;
}
$properties = $table_discount->getProperties(1);
$item = JArrayHelper::toObject($properties, 'JObject');
if (property_exists($item, 'params'))
{
$registry = new JRegistry;
$registry->loadString($item->params);
$item->params = $registry->toArray();
}
$data->discount = $item->discount_rate;
}
return $data;
}
Comments