0
votes

Im working on a pre created joomla component which using the MVC Architecture, My problem like this:

In Models i have a .php file with database fetch function as

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );


class class_name extends JModel
{

var $_data;

    function getlast_year(){
        $query = 'SELECT year FROM `table` ORDER BY year DESC LIMIT 0,1';
        $this->_db->setQuery( $query );
        return $this->_db->loadResult();
    }

}   

I added a new function to the same class file: (I have updated the table columns too in MVC /tables)

as:

function getAttendenceData()
{
    $query="SELECT id,octSec,octNin,octSect,octSec,octTwent FROM `table`";
        $this->_db->setQuery( $query );
        //$this->_data = $this->_db->loadObjectList();
        $this->_data = $this->_db->loadObject();
        return $this->_db->loadObjectList();
}

but in view i cant still access the fetched data from the above new function but older functions are working property

2
1. First check if you are actually getting some data from the query. do print_r($this->_db->loadObjectList()) in above function. 2. If yes, then what's the code in the view where you're accessing this data? - Vikk
Thanks Vikk query works fine. i access data as $this->data as $r then $r->id data fetches from older functions are fetched fine only the problem with the new function - Sudantha
In that case, can you post the code where you're retrieving data from model and assigning it to the view variable? This should be in the view.html.php file of your view. - Vikk
@Vikk This Components is so complex not possible to post the code :-) .. btw can u tell me how to retrieving data from model and assigning it to the view variable ? - Sudantha
@vikk there is some code as $items= & $this->get( 'Data'); - Sudantha

2 Answers

1
votes

This is not an actual answer but response to the comment.

First in your view.html.php file, you'll have to retrieve data from the model.

$attendance_data = & $this->get('AttendenceData');

This'll give you the object list as you are returning from your getAttendenceData() function.

Now assign it to a view variable (lets say data).

$this->assignRef('data', $attendance_data);

Now you can access this data in your view:

foreach($data as $r)
{
    echo $r->id;
}
1
votes

Isn't the problem that you are attempting to fetch the data twice?

With this line you retrieve it and store it locally in the class's _data variable.

$this->_data = $this->_db->loadObject();

With this line you attempt to retrieve the data again but you've already retrieved it (if there was only one result). You therefore are probably returning a false

return $this->_db->loadObjectList();

You should probably return $this->_data at the end of the function - assuming the original function you are copying was indeed functional.