Allow access an action without authonticate in cakephp

Cakephp

Posted on 17 Nov 15 by Vickey
  410

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() 
{
	parent::beforeFilter();
	$this->Auth->allow('login');
}

 

Why use recursive in cakephp find query

Cakephp

Posted on 06 Nov 15 by Vickey
  680

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

Cakephp

Posted on 06 Nov 15 by Vickey
  619

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(
	'conditions'=>array(
		'OR'=>array(
			'Mymodel.column1' => 2,
			'Mymodel.column2' => 5
		),
		'AND'=>array(
			'Mymodel.column3'=>1
		)
	))
);

 

Add validation on dynamically added form elements in JavaScript

Js

Posted on 14 Oct 15 by Vickey
  1175

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");
           break;
       }
	   else
	   {
            error = 0;
       }
    }
    if (error==0) {
        return true;
    }
    else{
    	return false; 
    }
}

 

Custom Role management in cakephp

Cakephp

Posted on 10 Oct 15 by Vickey
  541

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.

$permissions=[];
$permissions['default_permissions']=array(

    'USER'=>array('ADD'=>array(1),'EDIT'=>array(1),'INDEX'=>array(1,2))

);
$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

Cakephp

Posted on 10 Oct 15 by Vickey
  637

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

Cakephp

Posted on 10 Oct 15 by Vickey
  538

By using html helper we can create form elements in cakephp.here 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

$Countries=$this->Country->find('all',array('fields'=>array('Country.id','Country.name')));
foreach ($Countries as $country) {
 $countrylist[$country['Country']['id']]=$country['Country']['name'];
}
$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

Cakephp

Posted on 10 Oct 15 by Vickey
  325

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

Mysql

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

 

How to submit a form on press enter key

Jquery

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

 

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

 

www.000webhost.com