11
banesto
Re: Module Development tutorial (with classes)
  • 2007/1/12 22:57

  • banesto

  • Just popping in

  • Posts: 61

  • Since: 2005/1/24


and another question..

i have a class:
le="color: #000000"><?php class MembersType extends XoopsObject { function MembersType() { $this->XoopsObject(); $this->initVar('tid', XOBJ_DTYPE_INT, null, false); $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100); $this->initVar('climit', XOBJ_DTYPE_INT, null, false); $this->initVar('cname', XOBJ_DTYPE_TXTBOX, null, true, 100); } }


and in classhandler i want to use $this->getVar('name'); but that doesn't work, because $this directs to classhandler not class, so - how can i access needed variables using getVar ?
where's my red bull!

12
banesto
Re: Module Development tutorial (with classes)
  • 2007/1/12 23:11

  • banesto

  • Just popping in

  • Posts: 61

  • Since: 2005/1/24


man, i solved this too..

i had to use already written function "get" like this:

le="color: #000000"><?php $item = $this->get($id); $name = $item->getVar('name');


and then getVar works like heaven!
where's my red bull!

13
banesto
Re: Module Development tutorial (with classes)
  • 2007/1/13 1:18

  • banesto

  • Just popping in

  • Posts: 61

  • Since: 2005/1/24


how does this work? i mean how does foreach loop connects to the following operations? Variables $k and $v are not mentioned and yet somehow function can access variables like "$name", "$climit" etc..

le="color: #000000"><?php foreach ($MembersType->cleanVars as $k => $v) { ${$k} = $v; } if ($MembersType->isNew()) { $tid = $this->db->genId('acc_member_type'); $sql = sprintf("INSERT INTO %s (tid, name, climit, cname) VALUES (%u, %s, %u, %s)", $this->db->prefix('acc_member_type'), $tid, $this->db->quoteString($name), $climit, $this->db->quoteString($cname));}
where's my red bull!

14
macklein
Re: Module Development tutorial (with classes)
  • 2007/2/19 14:05

  • macklein

  • Just popping in

  • Posts: 21

  • Since: 2006/6/29


Quote:

banesto wrote:
how does this work? i mean how does foreach loop connects to the following operations? Variables $k and $v are not mentioned and yet somehow function can access variables like "$name", "$climit" etc..

le="color: #000000"><?php foreach ($MembersType->cleanVars as $k => $v) { ${$k} = $v; } if ($MembersType->isNew()) { $tid = $this->db->genId('acc_member_type'); $sql = sprintf("INSERT INTO %s (tid, name, climit, cname) VALUES (%u, %s, %u, %s)", $this->db->prefix('acc_member_type'), $tid, $this->db->quoteString($name), $climit, $this->db->quoteString($cname));}


if you have $k="foo", and you do ${$k}=$v;
then you have created a variable $foo=$v
this is usefull if u want to create variables named as the keys of an array automaticly

15
macklein
Re: Module Development tutorial (with classes)
  • 2007/2/19 14:09

  • macklein

  • Just popping in

  • Posts: 21

  • Since: 2006/6/29


Quote:

banesto wrote:
Thanks, that worked out!

How can i get actual results from "getObjects" function (it's in AccMembersTypeHandler class)?

le="color: #000000"><?php function getObjects($criteria = null, $id_as_key = false) { $ret = array(); $limit = $start = 0; $sql = 'SELECT * FROM '.$this->db->prefix('acc_member_type'); if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { $sql .= ' '.$criteria->renderWhere(); $limit = $criteria->getLimit(); $start = $criteria->getStart(); } $result = $this->db->query($sql, $limit, $start); if (!$result) { return $ret; } while ($myrow = $this->db->fetchArray($result)) { $MembersType = new MembersType(); $MembersType->assignVars($myrow); if (!$id_as_key) { $ret[] =& $MembersType; } else { $ret[$myrow['tid']] =& $MembersType; } unset($MembersType); } return $ret; }


$class_handler =& xoops_getmodulehandler('memberstype');
$memberstype =& $class_handler->getObjects;

... what's next? i get $ret back, right? how can i get out results from that?


$ret is an array of $XoopsObject's, so you wil have your result like this:
$ret[0]=an Object;
$ret[1]another Object;
and so on.........

16
macklein
Re: Module Development tutorial (with classes)
  • 2007/2/19 14:11

  • macklein

  • Just popping in

  • Posts: 21

  • Since: 2006/6/29


Quote:


and another question just for confirmation.. when defining a function and putting for example $id = false in the brackets like this

getAll($id = false)

means that this attribute is not necessary when calling?


It means that you could pass the attribute when calling , but if you don't, it will be taken as false (in this case)

17
banesto
Re: Module Development tutorial (with classes)
  • 2007/2/20 21:47

  • banesto

  • Just popping in

  • Posts: 61

  • Since: 2005/1/24


thanks for replies!

i've got some new question:

$this->db->getInsertid();

called this after INSERT query and no matter what the primary key value is, it returs 0.. what is wrong?
where's my red bull!

18
banesto
Re: Module Development tutorial (with classes)
  • 2007/2/21 13:16

  • banesto

  • Just popping in

  • Posts: 61

  • Since: 2005/1/24


oh, the sql wasn't executed.. shame on me.

but what about db->getid()? this returns 0 every time, even user class, when inserting new user. so what's use of it?
where's my red bull!

19
Dave_L
Re: Module Development tutorial (with classes)
  • 2007/2/21 19:59

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


[XOOPS 2.0.16]

Where do you see db->getid()?

XoopsMySQLDatabase::getInsertId() is defined in class/database/mysqldatabase.php, but I don't see a method named "getid".