$db->nameQuote('id')." = ".$this->_id;
$db->setQuery($query);
$this->_foobar = $db->loadObject();
}
// return the foobar data
return $this->_foobar;
}
}
Our model is now usable; we can retrieve a record from the table #__myextesnion_
foobar. How we choose to implement get methods is entirely up to us. There are
some common techniques used when implementing the get methods, but these
should only be used where appropriate.
Use a property to cache retrieved data:
var $_foobar;
Create a private method to load the data:
function _loadFoobar()
{
// Load the data
if (empty($this->_foobar))
{
??? ???
Chapter 4
[ 73 ]
$query = $this->_buildQuery();
$this->_db->setQuery($query);
$this->_foobar = $this->_db->loadObject();
return (boolean) $this->_foobar;
}
return true;
}
Create a private method to build a query string:
function _buildFoobar()
{
$db =& $this->getDBO();
return "SELECT * FROM "
.$db->nameQuote('#__myextension_foobar')
" WHERE ".$db->nameQuote('id') " = " .$this->_id;
}
Create a private method to build a blank set of data:
function initializeFoobar()
{
if (empty($this->_foobar))
{
$foobar = new stdClass;
$foobar->id = 0;
$foobar->name = null;
$this->_foobar =& $foobar;
}
}
Data that we access in a model does not have to come from the database.
Pages:
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109