Allow access an action without authonticate in cakephp


Posted on 17 Nov 15 by Vickey

In cakephp if we are using Auth component then we can not access any action without authonticate but if we need we can allow access any action by using 'allow' method. For example if i am not logedin and i want login then i need to access my login function but because of Auth componenet i can not access this then i can use the below code in my controller

public function beforeFilter() 


Why use recursive in cakephp find query


Posted on 06 Nov 15 by Vickey

In cakephp we use model binding or assoceation for binding models for example hasMany, belongsTo,hasOne.When we use cakephp query for a model by default it fetch all assoceated models data. but in some places we do not need the all models data we need the main model's data then we use recursive keyword. for example i have two tables one use 'Users' and second is 'Blogs', one use has many blogs so when i will use query in 'User' model cakephp by default fetch all related data from Blog also but i do not need it then i can force cakephp to fetch only User data by using  recursive=-1 and if i need more related data i will use recursive=0,recursive=1,recursive=2

$authors = $this->User->find('all','recursive'=>-1);
$authors = $this->User->find('all','recursive'=>0);
$authors = $this->User->find('all','recursive'=>1);
$authors = $this->User->find('all','recursive'=>2);


How to use AND and OR conditions in cakephp find function


Posted on 06 Nov 15 by Vickey

In cakephp find function we add conditions like this

$Mymodeldata=$this->Mymodel->find('first', array(
	'conditions'=>array('Mymodel.column1' => 2,'Mymodel.column2' => 5)

But in this code we have only 'AND' conditions. like in this sql query

'SELECT * FROM mytable WHERE column1=2 AND column2=5'

if you want to add 'AND' and 'OR' conditions both you can use the below code

$Mymodeldata=$this->Mymodel->find('first', array(
			'Mymodel.column1' => 2,
			'Mymodel.column2' => 5


Add validation on dynamically added form elements in JavaScript


Posted on 14 Oct 15 by Vickey

In some cases we add form elements by using DOM and we add multiple input box dynamically. For validate all fields we can use the below script. In the below code 'elementsclass' is a class of these elements, by this class we can get all elements and validate them one by one

function validate_form(){
    var error = 1;
    var time = document.getElementsByClassName("elementsclass");
    for (var i = 0; i < time.length; i++)
       if (time[i].value=='') {
           error = 1;
           alert("Please insert value");
            error = 0;
    if (error==0) {
        return true;
    	return false; 


Custom Role management in cakephp


Posted on 10 Oct 15 by Vickey

By using ACL we can manage roles in cakephp but if you dont know how it will be implement you can use a simple custom code.



$controller= strtoupper($this->params['controller']);
$action =strtoupper($this->params['action']);

if( isset($permissions['default_permissions'][$controller][$action]) && !in_array($this->Auth->user('role_id'),$permissions['default_permissions'][$controller][$action]))
return $this->redirect('/');

In this code USER is controller name and 'ADD' and 'EDIT' are actions. Here I have two types of users one is admin role id 1 and other is front end user role id is 2. So here admin can use 'ADD',  'EDIT', and 'index' page but normal use can use only 'INDEX'. Action that not given in 'permissions' array can access every one 

How to add conditions in cakephp paginate query


Posted on 10 Oct 15 by Vickey

For adding conditions in paginate query need to use Paginator settings

$this->Paginator->settings = array(
 'conditions' => array('User.role_id' => $id),
 'limit' => 10
$this->User->recursive = 0;
$this->set('users', $this->Paginator->paginate());


Create dropdown in cakephp by using option values from database


Posted on 10 Oct 15 by Vickey

By using html helper we can create form elements in i am creating a dropdown in which all option coming from database table

First use html helper in your controller

public $helper = array('Html');

now i am fetching data from database for use this in dropdown

foreach ($Countries as $country) {
$this->set('countrylist', $countrylist);

In  view

<?php echo $this->Form->input('User.country_id',array('type'=>'select','options'=>$countrylist)); ?>


How to use validation for unique value in cakephp


Posted on 10 Oct 15 by Vickey

In some cases we use a unique value for a database table fields for example in case of sign up we use unique value for our email or username so that there should not be create
more then one user with the same username or email.Cakephp provides us a very eassy validation rule 'isUnique' this rule we use in our model

'email' => array(
 'required' => array(
     'rule' => array('notEmpty'),
     'message' => 'You must enter a username.'

 'unique' => array(
     'rule'    => 'isUnique',
     'message' => 'This email has already been taken.'


How to get average value of a column in mysql query


Posted on 06 Oct 15 by Vickey
		SUM(`price`) / COUNT(*),2
	) AS average FROM `order` WHERE 1


How to submit a form on press enter key


Posted on 06 Oct 15 by Vickey
<script src="jquery.min.js"></script>
    $(document).keypress(function(e) {
    if (e.which==13) {


<form action="/" id="formid" method="get">
	<input name="text">