Cakephp important facts you must know – Part 3

  • You load a plugin model like this: $this->loadModel(“Forum.Topic”)
  • You output requestAction result using $this->requestAction(array(‘controller’ => ‘articles’, ‘action’ => ‘featured’), array(‘return’)); // see array(‘return’)
  • In controller, doing $this->set(‘first_name’) set variable $firstName for the view.
  • You can use HTML in  $html->link like this:
    echo $html->link(“Hello Dear jan”, array(‘controller’ => ‘categories’, ‘action’ => ‘view’, $topic['ForumCategory']['id']), null, false, false); //Note 5th argument “escape title” = false, check libs/view/helpers/html.php for more details
  • Use $text->truncate to truncate content with HTML, it does take care of HTML well provided you pass true in 5th argument. Example call: $text->truncate($text, $length = 100, $ending = ‘…’, $exact = true, $considerHtml = false)
  • If the targetted form action isn’t in the current controller, you can specify a URL for the form action using the ‘url’ key of the $options array. The supplied URL can be relative to your CakePHP application, or can point to an external domain. For example,<?php echo $form->create(null, array(‘url’ => ‘/recipes/add’)); ?>
    // or
    <?php echo $form->create(null, array(‘url’ => array(‘controller’ => ‘recipes’, ‘action’ => ‘add’))); ?>
  • You can print model errors dump in a controller like this:if(!$this->Model->save($this->data))    {
    pr($this->Model->validationErrors);
    }
  • You can always run db queries like this:$this->Profile>query(“delete from `users` where id=”.$this->Profile->id);
    //note the use of ‘Profile’ model. Any loaded model could be used to run a custom query on any other table of the current selected database.
  • Save single field value using saveField. For example:$this->User->id = $userId;
    $this->User->saveField(‘is_active’, “1″);
  • You can stop your controller action from rendering the view by using:
    $this->autoRender = false; //if can come handy particularily in case of ajax call where you dont want view to be rendered or no view at all.
  • You can use Configure::write(‘debug’, 0); within a controller action in case your debug derivative is set to greate than 0 in core.php file you dont want a particular action to show debug log at the bottom of page. This can come handy in case of ajax call. To turn off debug output for all ajax calls see Disable CakePHP debug output when using Ajax.

Possibly Related posts:

  1. Cakephp important facts you must know – Part 1
  2. Creating Ajax form in Cakephp using jQuery
  3. Disable CakePHP debug output when using Ajax