Model Class¶
The Model class provides functions to work with information in your database.
Creating a Model¶
Let's say you want to create bulletin board application. You might have a model class that contains functions to insert, update, and retrieve bulletin board application data. Here is an example of what such a model class might look like:
defined('V_IN_FRONTEND') or die('Powered by <a href="http://www.vesthelm.com/">Vesthelm EE</a>');
class Bulletin_Board_Bulletin extends Vesthelm_Model {
protected $_table = 'bulletin_board_bulletins'; //bulletins table
public function getBulletins() {
$this->select(array('b.title', 'b.description'))
->from(array('b' => $this->_table))
->where(array(array('status' => 'active')))
->limit(0, 100);
return $this->all();
}
public function get() {
$this->select(array('b.title', 'b.description'))
->from(array('b' => $this->_table))
->where(array('id' => 10));
return $this->row();
}
public function add() {
$data['title'] = 'Bulletin title';
$data['description'] = 'Bulletin description';
$additional['date'] = 'NOW()';
return $this->insert('', $data, $additional);
}
public function edit() {
$data['title'] = 'Bulletin title';
$data['description'] = 'Bulletin description';
return $this->update('', $data, array('id' => 10));
}
public function delete() {
$id = 10;
$id = is_array($id) ? $id : array($id);
return parent::delete($this->_table, array(array('id', 'IN', $id));
}
}
Anatomy of a Model¶
Model classes are stored in folders:
//Core models
example.com/frontend/models
example.com/acp/models
//Application models
example.com/applications/{application_name}/frontend/models
example.com/applications/{application_name}/acp/models
//Addon models
example.com/addons/{addon_name}/frontend/models
example.com/addons/{addon_name}/acp/models
//Sometimes models can be found here (old way):
example.com/addons/{addon_name}/frontend/classes
example.com/addons/{addon_name}/acp/classes
The basic prototype for a model class is:
class Enhancement_Name_Model_Name extends Vesthelm_Model {
function __construct(){
parent::__construct();
}
}
Where Enhancement_Name is the name of your enhancement. Enhancement names must have the first letter capitalized after underscore _ character.
Where Model_Name is the name of your class. Class names must have the first letter capitalized after underscore _ character. Make sure your class extends the base Model class.
The file name will be same as your class name (Model_Name). For example, if your class is:
class Bulletin_Board_Bulletin extends Vesthelm_Model {
function __construct(){
parent::__construct();
}
}
Your file will be:
example.com/applications/bulletin-board/acp/models/Bulletin.php
//or
example.com/applications/bulletin-board/acp/models/bulletin/Bulletin.php
Calling a Model¶
Calling core model:
$Addon_Model = $this->Addon;
Calling Bulletin Board application model:
$Bulletin_Model = $this->bulletin_board->Bulletin;
//identical
$Bulletin_Model->add();
$this->bulletin_board->Bulletin->add();
Where bulletin_board is the name of enhancement and Bulletin is the name of the Model.
Extending Core Model¶
Core model:
class Vesthelm_Category extends Vesthelm_Model {
function __construct() {
parent::__construct();
}
}
Extending:
class Bulletin_Board_Category extends Vesthelm_Category {
public function your_func() {}
}