Communicating with users of PHPShop.CMS Free and hitting them on the same “rake” pushed me to write useful tips.
Template engine
Useful tips on using the functions of template.
Tricks with set ()
The set () method assigns a template variable to be used in templates as
var @, example:
$var = " "; $this->set('my_var',$var);
and inserting its form @ my_var @ into the template tpl file will allow to display the phrase “We love Habr” on the site. Often, modules such as
formgenerator or
comment use interception and change variables. If we use the $ this-> set ('my_var', $ var) model in the hook function, then we completely rewrite the value of the variable, in our example we rewrite the page content. Everything would be fine, but this rule works if we have only one module enabled that intercepts the page content, and if we have more of them, then each new module will overwrite the output of the output of another module. In such cases, the third argument of the $ this-> set method, which takes the value true, comes to the rescue. Using this argument will allow not to rewrite the variable, but to supplement it, those will allow you to display the result of the modules work in turn - output of the form and comments.
Decision: $this->set('my_var',$var,true);
ParseTemplateReturn () in modules
If the module uses the template of this module, described in the module config.ini, then the entry is:
$comment=ParseTemplateReturn($GLOBALS['SysValue']['templates']['comment']['comment_content']);
will display an empty result. To use the global template function ParseTemplateReturn () in the modules, you must specify the second argument true, which switches the functions to the mode of reading template files from the modules folder, and not from the general phpshop / templates /.
')
Decision: ParseTemplateReturn($GLOBALS['SysValue']['templates']['comment']['comment_content'],true);
Using php in templates
Template engine allows you to use php-functions. Insert format:
@php ... php@
In order to avoid errors, the php compiler uses double quotes ", instead of single quotes, for example:
@php echo " !"; php@
Work with DB
Useful tips on the use of data access functions of the database.
Quick selection of module settings
There is a very good and short way to get the module settings that are stored in the modulename_systems database:
In this record we need to specify the name of the database from which we want to get data, for example
$this->objBase=$GLOBALS['SysValue']['base']['comment']['comment_system'];
and list the field names that we need, separated by commas
parent::PHPShopArray("enabled","flag","other");
To access this data is used:
$PHPShopCommentArray = new PHPShopCommentArray(); $enabled = $PHPShopCommentArray->getParam('enabled');
Complex queries in ORM
PHPShopOrm is used to work with MySQL. An example of a regular sample is:
$data=$PHPShopOrm->select(array('id','name'),array('id'=>'=10'),array('order'=>'id DESC'),array('limit'=>1000)); if(is_array($data)) foreach($data as $row){ .... }
This all simplifies and standardizes queries, but if you need to perform complex samples with the combination of tables? In such cases, the $ PHPShopOrm-> query () method is used, for example:
$result = $PHPShopOrm->query('SELECT a.*, b.login FROM '.$GLOBALS['SysValue']['base']['comment']['comment_log'].' AS a JOIN '.$GLOBALS['SysValue']['base']['users']['users_base'].' AS b ON a.user_id = b.id WHERE a.page="'.$page.'" order by a.id desc'); while($row = mysql_fetch_array($result)) { ..... }
Hash with multidimensionality select result ()
A frequent “side” gets in the way of the developer when, when sampling $ PHPShopOrm-> select () and provided that a record in DB 1 is a one-dimensional array, and if it is larger, it is multidimensional. To circumvent such uncertainty, the array ('limit' => 1) argument is used, in which the array will always be one-dimensional and work with it through a record of the form:
$data=$PHPShopOrm->select(array('id','name'),array('id'=>'=10'),array('order'=>'id DESC'),array('limit'=>1000)); if(is_array($data)) { .... }
in cases of obvious predisposition of the result to multiple fields, the recording format is used with the explicit indication of the limit of the array ('limit' => 1000):
$data=$PHPShopOrm->select(array('id','name'),array('id'=>'=10'),array('order'=>'id DESC'),array('limit'=>1000)); if(is_array($data)) foreach($data as $row){ .... }
Source
wiki.phpshopcms.ruUPD:
Part 2