Hello, everybody I'm Tarik not new to xoops (almost 2 years), I xoopsed some themes and created some of my own but now i want to upgrade my knowledge about module developing, and the basic thing to know when creating a Modules with poo - using classes instead of functions -
The tutos and resources about this two classes are quite few
here it will give you a start but the problem is when you want to develop on a certain level you need to have a full idea about the function of each class and the difference between them so i opened this tread - finally the reason
![](https://xoops.org/uploads/smil42e7a3d4c7b9f.gif)
- to expose some of the things that are flow to me and in the same time expose what i learned (maybe exporting the result to media wiki)
here is what i have got until now about these two classes:
Quote:
The object model in xoops is based on 2 classes XoopsObject and XoopsObjectHandler both encoded in the file /kernel/object.php. These two classes form the gateway to access to persistent data such as it is described in the DAO pattern. It is by specializing them we implement the model for a particular entity (often associated to a single table BD) as do all classes of the kernel directory which is the core of xoops. I strongly encourage developers of new modules based on xoops tables `BD using this model.
XoopsObject has all the services related to managing an object (an instance of the class) and attributes (getter and setter)
XoopsObjectHandler while serving manipulator or controller instances (insertion, modification or selection objects..)
(translated from here)
example of it (liaise)
class PmMessage extends XoopsObject
{
function __construct() {
$this->XoopsObject();
$this->initVar('msg_id', XOBJ_DTYPE_INT, null, false);
$this->initVar('msg_image', XOBJ_DTYPE_OTHER, 'icon1.gif', false, 100);
$this->initVar('subject', XOBJ_DTYPE_TXTBOX, null, true, 255);
$this->initVar('from_userid', XOBJ_DTYPE_INT, null, true);
$this->initVar('to_userid', XOBJ_DTYPE_INT, null, true);
$this->initVar('msg_time', XOBJ_DTYPE_INT, time(), false);
$this->initVar('msg_text', XOBJ_DTYPE_TXTAREA, null, true);
$this->initVar('read_msg', XOBJ_DTYPE_INT, 0, false);
$this->initVar('from_delete', XOBJ_DTYPE_INT, 1, false);
$this->initVar('to_delete', XOBJ_DTYPE_INT, 0, false);
$this->initVar('from_save', XOBJ_DTYPE_INT, 0, false);
$this->initVar('to_save', XOBJ_DTYPE_INT, 0, false);
}
function PmMessage(){
$this->__construct();
}
}
and the sql table that this class is intended to has this skeleton:
CREATE TABLE `pm_messages` (
`msg_id` int(10) unsigned NOT NULL auto_increment,
`msg_image` varchar(255) default NULL,
`subject` varchar(255) NOT NULL default '',
`from_userid` mediumint(8) unsigned NOT NULL default '0',
`to_userid` mediumint(8) unsigned NOT NULL default '0',
`msg_time` int(10) unsigned NOT NULL default '0',
`msg_text` text,
`read_msg` tinyint(1) unsigned NOT NULL default '0',
`from_delete` tinyint(1) unsigned NOT NULL default '1',
`from_save` tinyint(1) unsigned NOT NULL default '0',
`to_delete` tinyint(1) unsigned NOT NULL default '0',
`to_save` tinyint(1) unsigned NOT NULL default '0',
PRIMARY KEY (`msg_id`),
KEY to_userid (`to_userid`),
KEY inbox (`to_userid`,`read_msg`),
KEY outbox (`from_userid`, `read_msg`),
KEY prune (`msg_time`, `read_msg`, `from_save`, `to_delete`)
) TYPE=MyISAM;
so basically the class extended from xoopsobject
class Moduletable extends XoopsObject {
function __construct(){
$this->XoopsObject();
// key, data_type, value, req, max, opt
// foreach column
$this->initVar(key, data_type, value, req, max, opt);
}
function Moduletable() {
$this->__construct();
}
}
Data Types (for initVar function above):
* XOBJ_DTYPE_TXTBOX - Text Box
* XOBJ_DTYPE_TXTAREA - Text Area
* XOBJ_DTYPE_INT - Integer
* XOBJ_DTYPE_URL - URLs
* XOBJ_DTYPE_EMAIL - E-mails
* XOBJ_DTYPE_ARRAY - Arrays
* XOBJ_DTYPE_OTHER - Others (won't be cleaned)
* XOBJ_DTYPE_SOURCE -
* XOBJ_DTYPE_STIME - Short Time
* XOBJ_DTYPE_MTIME - Medium Time
* XOBJ_DTYPE_LTIME - Long Time
now come the xoopsobjecthandler
here is an example
class PmMessageHandler extends XoopsPersistableObjectHandler{
function __construct(&$db) {
parent::__construct($db, "priv_msgs", 'PmMessage', 'msg_id', 'subject');
}
function PmMessageHandler(&$db){
$this->__construct($db);
}
}
so the form is
class ModuletableHandler extends XoopsPersistableObjectHandler{
function __construct(&$db) {
parent::__construct($db, "table name", 'Moduletable', 'keyName', 'identifierName');
}
function ModuletableHandler(&$db){
$this->__construct($db);
}
}
all this is good when wanting to retrive an object
$name_handler =& xoops_getModuleHandler('Modulename', 'classname);
then retrieve the object with an id
$name =& $name_handler->get($id);
finally we can have values for each column of the table
$name->getVar('Key')
Now for inserting data
here is an example
$pm_handler =& xoops_getModuleHandler('message', 'pm');
$pm =& $pm_handler->create();
$pm->setVar("msg_time", time());
$pm->setVar("subject", $_POST['subject']);
$pm->setVar("msg_text", $_POST['message']);
$pm->setVar("to_userid", $_POST['to_userid']);
$pm->setVar("from_userid", $GLOBALS['xoopsUser']->getVar("uid"));
if (!$pm_handler->insert($pm)) {
echo $pm->getHtmlErrors();
}
so the first line is for acessing the handler
the second line is to creating a new object
then setting vars
to finally insert them
with getHtmlErrors() to have errors
all these things are simple
hope someone would get some info from tis
Tarik