General
- Use the most compact notation that is still easy to understand.
Arrays as Arguments
Where an array is required as an argument (used very commonly in CakePHP), define the array first and then pass it.
Preferred:
$args['fields'][] = "MAX(ordr)+1 as m"; $args['order'][] = "m"; $o = $this->CoEnrollmentAttribute->find('first', $args);
Not Preferred:
$o = $this->CoEnrollmentAttribute->find('first', array('fields' => array("MAX(ordr)+1 as m")), array('order' => array("m")));
Brackets and Indentation
- Use two (2) spaces for indentation.
- No tabs, only spaces.
- Curly brackets on new line:
if($a > 0) { $foo = bar; } else { $foo = fum; }
- Curly brackets may be omitted only when they may be omitted from the entire clause:
OK: if($a > 0) return(true); elseif($b > 0) return(false); Not OK: if($a > 0) { $a++; $foo = bar; } else return(false);
Methods
- Camel case with lower initial:
$this->bindModel(); $this->initializeParentCou();
SQL Query Optimization
In general, use Containable Behavior to constrain what Cake is pulling from the database. Cake will usually try to pull a bunch of associated data, which may or may not match what you actually need in a given context. You can use Containable to specify exactly which associated data you want returned.
Don't return anything but CoPerson:
$this->CoPerson->contain();
Obtain OrgIdentity and Name:
$this->OrgIdentity->contain('Name');
Obtain OrgIdentity, Name, CO, and CO Groups the Org Identity is a member of:
$args['contain'][] = 'Name'; $args['contain']['CoOrgIdentityLink']['CoPerson'][0] = 'Co'; $args['contain']['CoOrgIdentityLink']['CoPerson']['CoGroupMember'] = 'CoGroup'; $orgIdentities = $this->OrgIdentity->find('all', $args);
Variables
- Camel case with lower initial:
$cou = 'whatever'; $couAllowed = array(); $isInCou = false;
- Avoid very short names except in very compact contexts.