Skip to content

Instantly share code, notes, and snippets.

@saji89
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saji89/4cf4f84fece063824b09 to your computer and use it in GitHub Desktop.
Save saji89/4cf4f84fece063824b09 to your computer and use it in GitHub Desktop.
Cake PHP Conventions

Controller class names

Plural, CamelCased, and end in Controller


Multiword Controller URLS's

Multiple word controllers can be any ‘inflected’ form which equals the controller name so:

/redApples
/RedApples
/Red_apples
/red_apples

will all resolve to the index of the RedApples controller.

Actual convention for multiword URL's

The convention is that your URLs are lowercase and underscored, therefore /red_apples/go_pick is the correct form to access the RedApplesController::go_pick action.


File and Class Name Conventions

In general, filenames match the class names, which are CamelCased. . So if you have a class MyNiftyClass, then in CakePHP, the file should be named MyNiftyClass.php. Below are examples of how to name the file for each of the different types of classes you would typically use in a CakePHP application:

  • The Controller class KissesAndHugsController would be found in a file named KissesAndHugsController.php
  • The Component class MyHandyComponent would be found in a file named MyHandyComponent.php
  • The Model class OptionValue would be found in a file named OptionValue.php
  • The Behavior class EspeciallyFunkableBehavior would be found in a file named EspeciallyFunkableBehavior.php
  • The View class SuperSimpleView would be found in a file named SuperSimpleView.php
  • The Helper class BestEverHelpe would be found in a file named BestEverHelper.php

Courtesy: http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html


Find where id is not null

<?php
$this->User->find( 'all', array( 
    'conditions' => array("not" => array ( "User.site_url" => null)
))

Courtesy: http://stackoverflow.com/a/1197000/749232


Multiple conditions in CakePHP Query

<?php
if ( ! empty($projectid)) {
    $listOptions['conditions'] = array(
        'OR'=>array(
            array(
                'project_id' => $projectid,
                'not'=>array('project_id' => null)
            ),
            array('project_id' => null)
        )
    );
}

$auditList = $this->Audit->find('all', $listOptions);
-- The resultant query will look like:

SELECT `Audit`.`id`, `Audit`.`event`, `Audit`.`model`, `Audit`.`entity_id`, `Audit`.`project_id`, `Audit`.`json_object`, `Audit`.`description`, `Audit`.`user_id`, `Audit`.`entity_name`, `Audit`.`created` 
FROM `galaxy3`.`audits` AS `Audit`   
WHERE (
    (
        (
            (`project_id` = '1234')  AND  (
                NOT (`project_id` IS NULL)
                )
            )
        ) 
    OR (`project_id` IS NULL)
)

Courtesy:


Load model from a plugin in another controller

<?php
     $this->loadModel('<pluginname>.<modelname>');
 ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment