Page 1 of 1

A few beginner question from the new module dev tutorial

Posted: Wed Jan 06, 2016 11:23 am
by Guido
Since I've spent a lot of time learning PHP the past year, I want to become an active developer for CMSMS. I'm reading the new module development tutorial (thanks for this), and thought I'd ask some questions as I'm reading it to be able to learn the app's secrets.

My first one:

If I understand correctly, all files prefixed as 'action' (so action.do_something.php) are the handler files. They handle any data manipulation and database interaction. After this, you display a Smarty template file with the results. Now for my question, is it mandatory to name the Smaty template files the same as the accompanying action/handler file? So in the example, there's a handler file called action.edit_holiday.php. Is it mandatory to name the smarty template file 'edit_holidat.tpl'? Or is this just a good idea to keep thing organised?

Re: A few beginner question from the new module dev tutorial

Posted: Wed Jan 06, 2016 11:35 am
by Guido
My second question:

I see there is a method called: 'Insert_ID()', which is a method of the database object. I looked at the api documentation, but couldn't find a database class. I'd like to know what this method does, and why it's called separately from the insertion from the rest of the record.

EDIT
I read there is a 'CmsDbQueryBase' class, but I can't find that on this page: http://www.cmsmadesimple.org/APIDOC2_0/ ... s/CMS.html

Re: A few beginner question from the new module dev tutorial

Posted: Wed Jan 06, 2016 12:04 pm
by Guido
My third question:

Code: Select all

$holidays = $query->GetMatches();
I see this method used, which is a method if the 'LayoutTemplateQuery' class. It doesn't accept any arguments, am I correct to assume that this method knows from which module it's being called, so that it returns the records for this module automatically?

Re: A few beginner question from the new module dev tutorial

Posted: Wed Jan 06, 2016 12:12 pm
by Guido
Shouldn't the following line on page 23:

Code: Select all

$obj = new HolidayItem;
be:

Code: Select all

$obj = new HolidayItem();
?

EDIT
No, that's optional, didn't know that but I just looked it up on PHP.net

Re: A few beginner question from the new module dev tutorial

Posted: Wed Jan 06, 2016 12:37 pm
by Guido
I don't fully understand this part:

Code: Select all

public function execute()
{
if( !is_null($this->_rs) ) return;
$sql = 'SELECT SQL_CALC_FOUND_ROWS H.*
FROM '.CMS_DB_PREFIX.'mod_holidays H ORDER BY the_date DESC';
$db = \cms_utils::get_db();
$this->_rs = $db->SelectLimit($sql,$this->_limit,$this->_offset);
IF( $db->ErrorMsg() ) throw new \CmsSQLErrorException($db->sql.' -- '.$db->ErrorMsg());
$this->_totalmatchingrows = $db->GetOne('SELECT FOUND_ROWS()');
}
You assign the result set to $this->_rs, (I'm guessing the underscore is a CMSMS naming convention?), what is the actual query in which the rows are returned? Is it:

Code: Select all

$this->_totalmatchingrows = $db->GetOne('SELECT FOUND_ROWS()');
The 'FOUND_ROWS();' is a MySQL function I'm guessing, (will investigate this) but how does it know what rows to select? You'd think it wants to base it on the result set, but '$this->_rs' isn't mentioned in this call.

Re: A few beginner question from the new module dev tutorial

Posted: Wed Jan 06, 2016 2:02 pm
by Jo Morg
Guido wrote: Is it mandatory to name the smarty template file 'edit_holidat.tpl'? Or is this just a good idea to keep thing organised?
It's jut a good practice.
Guido wrote:My second question:

I see there is a method called: 'Insert_ID()', which is a method of the database object. I looked at the api documentation, but couldn't find a database class. I'd like to know what this method does, and why it's called separately from the insertion from the rest of the record.
CMSMS is using ADOdb Lite 3rd party library for the db interactions, although it will slowly move away from it on the new versions. It should keep backwards compatibility as much as possible though. The $db object referenced there comes from that lib.
Docs on external site: http://adodblite.sourceforge.net/functions.php
Guido wrote: I read there is a 'CmsDbQueryBase' class, but I can't find that on this page: http://www.cmsmadesimple.org/APIDOC2_0/ ... s/CMS.html
It is not documented on the API (yet) but the best documentation is looking at the code itself :). Peek the file and look at the comments: it's fairly well documented.
Guido wrote:My third question:

Code: Select all

$holidays = $query->GetMatches();
I see this method used, which is a method if the 'LayoutTemplateQuery' class. It doesn't accept any arguments, am I correct to assume that this method knows from which module it's being called, so that it returns the records for this module automatically?
No, it's not a method of LayoutTemplateQuery class, but of HolidayQuery class. It seems that there is a line missing there, on the original code of the tutorial, the $query->execute(): call, which would execute the query itself, just before the $holidays = $query->GetMatches();... probably like:

Code: Select all

$query = new HolidayQuery;
$query->execute();
$holidays = $query->GetMatches();
But I'll leave it to the author to clarify that.

HTH

Re: A few beginner question from the new module dev tutorial

Posted: Wed Jan 06, 2016 3:05 pm
by calguy1000
Nope. This is correct.

Code: Select all

$query = new HolidayQuery;
$holidays = $query->GetMatches();
The GetMatches() method calls the MoveFirst() method which calls the execute() method... so all of the internals of executing the query are handled.

The CmsDbQuery base class contains a 'resultset/recordset' object, and has wrappers for many of the methods in that class. Derived classes are responsible for converting each and every returned row into an object of some type for returning to the application.

http://www.cmsmadesimple.org/APIDOC2_0/ ... yBase.html

Re: A few beginner question from the new module dev tutorial

Posted: Wed Jan 06, 2016 5:00 pm
by Jo Morg
calguy1000 wrote:Nope. This is correct.

Code: Select all

$query = new HolidayQuery;
$holidays = $query->GetMatches();
The GetMatches() method calls the MoveFirst() method which calls the execute() method... so all of the internals of executing the query are handled.
I suspected as much but missed that bit in the code itself when I looked. Makes sense and makes the code cleaner too :) .

Re: A few beginner question from the new module dev tutorial

Posted: Wed Jan 06, 2016 10:46 pm
by Guido
Thanks, I'll study you answers as soon as I get the time.