Path: pluginkey/install.php
$sql = "CREATE TABLE `" . OW_DB_PREFIX . "pluginkey_tablename` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`email` VARCHAR(200) NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE=MyISAM
ROW_FORMAT=DEFAULT";
OW::getDbo()->query($sql);
By calling OW::getDbo you get OW_Database object for working with database which is of lower level than OW_BaseDao. Class OW_Database is a wrapper for PDO. In order to perform a database query you can use query method of OW_Database class having passed an sql query as a parameter.
Note: The core advantage of PDO over MySQLi is in its database driver support. At the time of this writing, PDO supports 12 different drivers, opposed to MySQLi, which supports MySQL only. Read more
Note: Always use the OW_DB_PREFIX constant (you entered during the installation) when creating or querying tables.
Path: pluginkey/bol/tablename.php
class PLUGINKEY_BOL_Tablename extends OW_Entity
{
/**
* @var string
*/
public $email;
}
The rule for naming the DTO (Data Transfer Object) class and file is identical to the one for controllers except for one thing - CTRL suffix should be replaced with BOL. Our DTO should be inherited from OW_Entity class.
DAO is required for interacting with database. Roughly speaking, DAO maps data from the table to DTO and back.
class CONTACTUS_BOL_DepartmentDao extends OW_BaseDao
{
/**
* Constructor.
*
*/
protected function __construct()
{
parent::__construct();
}
/**
* Singleton instance.
*
* @var PLUGINKEY_BOL_TablenameDao
*/
private static $classInstance;
/**
* Returns an instance of class (singleton pattern implementation).
*
* @return PLUGINKEY_BOL_TablenameDao
*/
public static function getInstance()
{
if ( self::$classInstance === null )
{
self::$classInstance = new self();
}
return self::$classInstance;
}
/**
* @see OW_BaseDao::getDtoClassName()
*
*/
public function getDtoClassName()
{
return 'PLUGINKEY_BOL_Item';
}
/**
* @see OW_BaseDao::getTableName()
*
*/
public function getTableName()
{
return OW_DB_PREFIX . 'pluginkey_tablename';
}
}
The rule for naming the DAO class and file is identical to the one for controllers with one exception - CTRL suffix should be changed to BOL. Our DAO class is inherited from OW_BaseDao abstract class, and is a singleton. OW_BaseDao contains a number of useful methods for working with database.
This class is also a singleton to save server resources. The difference between service and DAO is that service calls methods from DAO (not knowing how data is stored) and executes other business logic. There is no business logic in DAO, there are only database queries.
class CONTACTUS_BOL_Service
{
/**
* Singleton instance.
*
* @var PLUGINKEY_BOL_Service
*/
private static $classInstance;
/**
* Returns an instance of class (singleton pattern implementation).
*
* @return PLUGINKEY_BOL_Service
*/
public static function getInstance()
{
if ( self::$classInstance === null )
{
self::$classInstance = new self();
}
return self::$classInstance;
}
private function __construct()
{
}
// Some plugin related methods
public function getTableItemLabel( $id )
{
return OW::getLanguage()->text('pluginkey', $this->getItemKey($id));
}
public function addTableItem( $email, $label )
{
$contact = new PLUGINKEY_BOL_Tablename();
$contact->email = $email;
PLUGINKEY_BOL_TablenameDao::getInstance()->save($contact);
BOL_LanguageService::getInstance()->addValue(
OW::getLanguage()->getCurrentId(),
'pluginkey',
$this->getItemKey($contact->id),
trim($label));
}
public function deleteTableItem( $id )
{
$id = (int) $id;
if ( $id > 0 )
{
$key = BOL_LanguageService::getInstance()->findKey('pluginkey', $this->getItemKey($id));
BOL_LanguageService::getInstance()->deleteKey($key->id, true);
PLUGINKEY_BOL_TablenameDao::getInstance()->deleteById($id);
}
}
private function getTableItemKey( $name )
{
return 'dept_' . trim($name);
}
public function getTableItemList()
{
return PLUGINKEY_BOL_DepartmentDao::getInstance()->findAll();
}
It returns an instance of PLUGINKEY_BOL_TablenameDao class and calls a standard findAll() method that returns an array of PLUGINKEY_BOL_Tablename objects.
At first the PLUGINKEY_BOL_Tablename() class object gets created and then an email value gets set in it. Using the save() method of PLUGINKEY_BOL_TablenameDao class the object will be saved.
Concluding a label for the new-created table item will be added to Languages. This can be done with addValue method of BOL_LanguageService class instance.