1
baloch
how can i create and use xoops object handler in my module?
  • 2009/6/24 8:12

  • baloch

  • Just popping in

  • Posts: 34

  • Since: 2006/3/22


I am developing a module i want to use XOOPS Object handler for creating and updating the records in database. But can please any one explain to me how the objecthandler (module_handler) class works?? How can i extend the class?


2
bumciach
Re: how can i create and use xoops object handler in my module?
  • 2009/6/24 11:16

  • bumciach

  • Not too shy to talk

  • Posts: 153

  • Since: 2007/6/25


I'm at a similar stage in developing. It was very helpful for me to look up the code of XoopsFAQ module by Catzwolf.
https://xoops.org/modules/newbb/viewtopic.php?topic_id=68221&start=0

3
baloch
Re: how can i create and use xoops object handler in my module?
  • 2009/6/24 12:37

  • baloch

  • Just popping in

  • Posts: 34

  • Since: 2006/3/22


I went through Faq module by catzwolf it is using normal quries. I am trying to understand and if anyone here to explain me how to extend xoopsobjecthandler class to create module_handler. or may be i am not understanding how to explain properly.

4
trabis
Re: how can i create and use xoops object handler in my module?
  • 2009/6/24 13:00

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


Don't extend XoopsObjectHandler, extend XoopsPersitableObjectHandler

a very simple class located at modules/mymenus/class/menus.php:

//  Author: Trabis
//  URL: http://www.xuups.com
//  E-Mail: lusopoemas@gmail.com

if (!defined("XOOPS_ROOT_PATH")) {
    die(
"XOOPS root path not defined");
}

class 
MymenusMenus extends XoopsObject
{
    
/**
     * constructor
     */
    
function MymenusMenus()
    {
        
$this->__construct();
    }

    function 
__construct()
    {
        
$this->initVar("id"XOBJ_DTYPE_INT);
        
$this->initVar('title'XOBJ_DTYPE_TXTBOX);
    }
}

class 
MymenusMenusHandler extends XoopsPersistableObjectHandler
{
    function 
MymenusMenusHandler(&$db)
    {
        
$this->__construct($db);
    }

    function 
__construct(&$db)
    {
        
parent::__construct($db'mymenus_menus''MymenusMenus''id''title');
    }
}


Now to access this handler I just need to do this:
$obj_handler =& xoops_getModuleHandler('menus''mymenus');


I can now use $obj_handler to do any CRUD operations on my 'menus' table without need to code a single query!

Take a look at kernel/xoopsobject to see available methods. Here are SOME examples:
$obj_handler->get($id null$fields null);

$obj_handler->insert(&$object$force true);

$obj_handler->delete(&$object$force false);

$obj_handler->deleteAll($criteria null$force true$asObject false);

$obj_handler->updateAll($fieldname$fieldvalue$criteria null$force false);

$obj_handler->getObjects($criteria null$id_as_key false$as_object true);

$obj_handler->getAll($criteria null$fields null$asObject true$id_as_key true);
   
$obj_handler->getList($criteria null$limit 0$start 0); 

$obj_handler->getCount($criteria);


Is very important you learn how Criteria and CriteriaCompo classes work. XPOH will take care of sanitation while managing objects but, there is no sanitation on fields that go trough the $criteria. You can look at this classes in class/criteria.php.

Here is a simple example on how object relates to object_handler after a form submit event(here we are creating a new row in database).

$this_handler =& xoops_getModuleHandler('menus','mymenus');
    
$obj $this_handler->create();
    
$obj->setVars($_POST);

    if (!
$this_handler->insert($obj)){
        
$msg _AM_MYMENUS_MSG_ERROR;
    }else{
        
$msg _AM_MYMENUS_MSG_SUCCESS;
    }



5
baloch
Re: how can i create and use xoops object handler in my module?
  • 2009/6/24 14:41

  • baloch

  • Just popping in

  • Posts: 34

  • Since: 2006/3/22


ok thanks a lot understood the object handler, about criteria still confused. is critirea about setting like where clause, limit, orderby, groupy etc or something else?

6
trabis
Re: how can i create and use xoops object handler in my module?
  • 2009/6/25 12:40

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


Quote:

baloch wrote:
is critirea about setting like where clause, limit, orderby, groupy etc or something else?


Yes!
There are two classes in class/criteria.php:

Criteria() - Lets you create ONE condition for using in the "where" section of the query
CriteriaCompo() - Lets you use one or more Criteria and allows you to set limit, start, sortby and orderby for your query.

Both classes are available globaly after the inclusion of mainfile.php so you can use them without need to include files.

All you need is to do this:

$criteria = new Criteria('uid'3);
//This will be translated in: where uid=3

$criteria = new Criteria('uid'3'>');
//This will be translated in: where uid>3

$criteria = new Criteria('uid''(3, 5, 8)''IN');
//This will be translated in: where uid IN(3, 5, 8)



The best part is CriteriaCompo:
$criteria1 =  new Criteria('uid''<>');
$criteria2 =  new Criteria('post'0'>');

$criteria = new CriteriaCompo();
$criteria->add($criteria1);
$criteria->add($criteria2'OR');//'AND' is used by default

//this will result in : where (uid<>3 OR post>0)

//other options
$criteria->setSort('uname');
$criteria->setOrder('ASC');
$criteria->setLimit(10);
$criteria->setStart(0);



Criteria and Handlers are good friends:
$myObjects $myhandler->getObjects($criteria);


Don't forget that Criteria does not have add() and setLimit() etc. Criteria is just for a single condition. If you need more power allways use CriteriaCompo().

You can pass a Criteria directly to CriteriaCompo like this:
$criteria = new CriteriaCompo(new Criteria('uid''<>'));
$criteria->setLimit(10);
$myObjects $myhandler->getObjects($criteria);


Hope you found it easy to understand!

7
baloch
Re: how can i create and use xoops object handler in my module?
  • 2009/6/25 12:50

  • baloch

  • Just popping in

  • Posts: 34

  • Since: 2006/3/22


Thanks Trabis for your easy explanation. Trabis The Xoops's Future...

8
iHackCode
Re: how can i create and use xoops object handler in my module?

Trabis can you add some of those examples to the wiki .
https://xoops.org/modules/mediawiki/index.php?title=Dev:XoopsCriteria
CBB / LatestNews / Publisher / XM-Spotlight

(ノ◕ヮ◕)ノ*:・゚✧

9
baloch
Re: how can i create and use xoops object handler in my module?
  • 2009/6/25 23:41

  • baloch

  • Just popping in

  • Posts: 34

  • Since: 2006/3/22


Hi Trabis sorry to worry u again i tried the method u shown above to retrieve data from database but i am getting this error
atal error: Call to a member function getVars() on a non-object in C:\xampp\htdocs

here is my class
if (!defined("XOOPS_ROOT_PATH")) {
    die(
"XOOPS root path not defined");


//create the xoopsobject for crud opeation
class AudioAudio extends XoopsObject

    function 
AudioAudio()
    {
    
        
$this->__construct();
    
    }
    
    function 
__construct()
    {
        
$this->initVar("id"XOBJ_DTYPE_INT);
        
$this->initVar("source"XOBJ_DTYPE_TXTBOX);
        
$this->initVar("title"XOBJ_DTYPE_TXTBOX);
        
$this->initVar("download"XOBJ_DTYPE_TXTBOX);
        
$this->initVar("size"XOBJ_DTYPE_TXTBOX);
        
$this->initVar("type"XOBJ_DTYPE_TXTBOX);
        
$this->initVar("date"XOBJ_DTYPE_TXTBOX);
    }
}

class 
AudioAudioHandler extends XoopsPersistableObjectHandler
{
    function 
AudioAudioHandler(&$db)
    {
        
$this->__construct($db);
    }

    function 
__construct(&$db)
    {
        
parent::__construct($db'audios''AudioAudio''id');
    }
    
    

}
?>


here i am trying to retireve
$obj_handler =& xoops_getModuleHandler('audio','audio');
$obj $obj_handler->getAll($criteria null$fields null$asObject true$id_as_key true); 
echo 
"".$obj->getVars("source")."
    "
.$obj->getVars("title")."
    "
.$obj->getVars("type")."
    "
.$obj->getVars("date")."";



in a debug it shows select * from audios but it i canot list the data due to the error above

10
trabis
Re: how can i create and use xoops object handler in my module?
  • 2009/6/26 0:03

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


The handler implements classes listed on class/model
The getAll method is listed in model/read.php

Take a look:
/**
     * get all objects matching a condition
     * 
     * @param   object      $criteria {@link CriteriaElement} to match
     * @param   array       $fields     variables to fetch
     * @param   bool        $asObject     flag indicating as object, otherwise as array
     * @param   bool        $id_as_key use the ID as key for the array
     * @return  array of objects/array {@link XoopsObject}
     */
    
function &getAll($criteria null$fields null$asObject true$id_as_key true)


As you see this method returns an array of objects or an array of arrays(depending on how you set $asObject)

Even if you are expecting one single result you will have to use:
$obj[0]->getVar("title"); 
//and not getVars, getVars return an array and not the field, it does not even accept arguments.



But if you want objects there is no need to use getAll()
You can do this:
$obj_handler =& xoops_getModuleHandler('audio','audio');
$objs $obj_handler->getObjects(nulltrue); //no criteria and use key(in this case 'id') for each array
foreach ($objs as $key => $obj) {
    echo 
"".$obj->getVar('source')."
    "
.$key/*or $obj->getVar('id')*/."
    "
.$obj->getVar('title')."
    "
.$obj->getVar('type')."
    "
.$obj->getVar('date')."";
}


if you do not need key:

$obj_handler =& xoops_getModuleHandler('audio','audio');
$objs $obj_handler->getObjects();
foreach (
$objs as $obj) {
    echo 
"".$obj->getVar('source')."
    "
.$obj->getVar('title')."
    "
.$obj->getVar('type')."
    "
.$obj->getVar('date')."";
}


or if you prefer (good for assign to a template or to a block):

$obj_handler =& xoops_getModuleHandler('audio','audio');
$objs $obj_handler->getObjects();
foreach (
$objs as $obj) {
    
$array $obj->getValues();
    
//$block['content'] = $array;
    //$xoopsTpl->assign('content', $array);
    
echo "".$array['source']."
    "
.$array['title']."
    "
.$array['type']."
    "
.$array['date']."";
}


Notice the use of getValues instead of getVars.
getVars does no sanitation for display while getValues does!

Login

Who's Online

342 user(s) are online (297 user(s) are browsing Support Forums)


Members: 0


Guests: 342


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits