Accessing the Database
You may often want to query or update your database from within your add-on. This can be done using the Model Service or you can also execute SQL statements by using the legacy Database Driver.
The Model Service is much cleaner than the legacy Database Driver. However, it also has limitations on what it can do compared to the Database Driver.
Let’s use a real example to show how you might access data using both methods:
We will use the Member model to show a list of members. For this we will add a template tag called memberlist
to our add-on named “Amazing Add-On”. The tag syntax will be this:
{exp:amazing_add_on:memberlist}
Here is the class syntax using the Model Service:
namespace ExpressionengineDeveloper\AmazingAddOn\Module\Tags;
use ExpressionEngine\Service\Addon\Controllers\Tag\AbstractRoute;
class Memberlist extends AbstractRoute
{
public $return_data = '';
// Example tag: {exp:amazing_add_on:memberlist}
public function process()
{
$members = ee('Model')->get('Member')->all();
foreach($members as $member)
{
$this->return_data .= $member->screen_name."<br>";
}
return $this->return_data;
}
}
Here is the class syntax using the legacy Database Drive:
namespace ExpressionengineDeveloper\AmazingAddOn\Module\Tags;
use ExpressionEngine\Service\Addon\Controllers\Tag\AbstractRoute;
class Memberlist extends AbstractRoute
{
public $return_data = '';
// Example tag: {exp:amazing_add_on:memberlist}
public function process()
{
ee()->db->select('screen_name');
$query = ee()->db->get('members');
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$this->return_data .= $row->screen_name."<br>";
}
}
return $this->return_data;
}
}
Both of these examples would produce a list similar to below:
admin
Moss
Jen
Roy
Douglas
Richmond
This is only the beginning of how you can interact with the database through your add-on. Explore the Model Service and the legacy Database Driver to learn how to add more power to your add-on.