Back to Tutorial Home

Live Cheat Sheet

Click a link below, then copy the code sample from this text box.



Database

Get a database handle
$db = Cgn_Db_Connector::getHandle();
Get a database handle (different DSN)
$db = Cgn_Db_Connector::getHandle('custom');
New Data Item (active record)
$item = new Cgn_DataItem('table', 'table_id'); $item->load(1);
New Data Item Finder
$finder = new Cgn_DataItem('table', 'table_id'); $finder->andWhere('column', '1'); $results = $finder->find();
New Data Item Finder with Joins
$finder = new Cgn_DataItem('table', 'table_id'); $finder->andWhere('column_one', '100', '>'); $finder->hasOne('table_B', 'B_col_fkey', 'table_B_alias'); $finder->hasOne('table_C', 'C_col_fkey', 'table_C_alias', 'local_col'); $finder->andWhere('table_B_alias.col_two', 'column_one'); $finder->andWhere('table_C_alias.col_three', $someValue); $finder->orderBy('local_col DESC'); $finder->_cols = array('table.*', 'table_B_alias.column_two'); //use primary key as array index $finder->_rsltByPkey = TRUE; $records = $finder->find();

Services

Skeleton Service
<?php /** * New service * @package CHANGE */ class Cgn_Service_CHANGE_ME extends Cgn_Service { function __construct() { } function mainEvent(&$req, &$t) { $t['message'] = "This is the main event."; } }
Skeletal Trusted Service (Spam filter)
<?php /** * New service * @package CHANGE */ class Cgn_Service_CHANGE_ME extends Cgn_Service_Trusted { var $untrustLimit = 3; var $entry = NULL; var $usesConfig = TRUE; var $dieOnFailure = TRUE; function __construct() { $this->screenPosts(); $this->trustPlugin('throttle',10); $this->trustPlugin('html',10); // $this->trustPlugin('requireCookie'); // $this->trustPlugin('secureForm'); } function mainEvent(&$req, &$t) { $t['message'] = "This is the main event."; } }
Check the spam rating of a request to a service.
$spamScore = $this->getSpamScore();
Redirect a service
$this->presenter = 'redirect'; $t['url'] = cgn_appurl('module', 'service', 'event', array('get1'=>'value')); return false;
Redirect a service to its own home.
$this->redirectHome(); return false;

Users and sessions

Get the current user (inside service)
$u = $req->getUser();
Get the current user (from anywhere)
$u = Cgn_SystemRequest::getUser();
Get the current session object
$session = Cgn_Session::getSessionObj();

URLs

Create a URL to an application
$href = cgn_appurl('module', 'service', 'event', array('get1'=>'value'));

Template

Show a template section by name
<?= Cgn_Template::parseTemplateSection('content.main');?>
Check if a template section has content
<? if( Cgn_Template::sectionHasContent('content.main') ) { } ?>
Change the current page's title
Cgn_Template::setPageTitle("title");
Change the site's tag line
Cgn_Template::setSiteTagLine("put all your fancy quotes here.");

Ini Settings

Create a new section callback (layout.ini)
;use boot/local/layout.ini for local changes [object] column.leftside=@lib.path@/lib_cgn_layout.php:Cgn_LayoutManager:defaultLayoutManager:showMainContent
Create a new database connection (core.ini)
;use boot/local/core.ini for local changes [dsn] custom.uri=mysql://user:password@localhost/cognifty

MVC

Create a new MVC Table (array records)
include_once(CGN_LIB_PATH.'/html_widgets/lib_cgn_widget.php'); include_once(CGN_LIB_PATH.'/lib_cgn_mvc.php'); include_once(CGN_LIB_PATH.'/lib_cgn_mvc_table.php'); $list = new Cgn_Mvc_TableModel(); //cut up the data into table data foreach ($recordList as $record) { $list->data[] = array( cgn_applink( $record['title'], 'module','service','event',array('id'=>$record['table_id'])), $record['caption'], $record['sub_type'], cgn_applink('edit','module','service','edit',array('id'=>$record['table_id'])), cgn_applink('delete','module','service','del',array('table_id'=>$record['table_id'],'table'=>'table')), ); } $list->headers = array('Title','Sub-Title','Version','Sub-Type','Edit','Delete'); $t['table'] = new Cgn_Mvc_TableView($list);
Create a new MVC Table (data items)
include_once(CGN_LIB_PATH.'/html_widgets/lib_cgn_widget.php'); include_once(CGN_LIB_PATH.'/lib_cgn_mvc.php'); include_once(CGN_LIB_PATH.'/lib_cgn_mvc_table.php'); $list = new Cgn_Mvc_TableModel(); //cut up the data into table data foreach ($objectList as $object) { $list->data[] = array( cgn_applink( $object->title, 'module','service','event',array('id'=>$object->getPrimaryKey())), $object->caption, $object->sub_type, cgn_applink('edit','module','service','edit',array('id'=>$object->getPrimaryKey())), cgn_applink('delete','module','service','del',array('table_id'=>$object->table_id,'table'=>$object->_table)), ); } $list->headers = array('Title','Caption','Version','Sub-Type','Edit','Delete'); $t['table'] = new Cgn_Mvc_TableView($list);