Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • CakePHP find condition examples

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 758
    Comment on it

    This blog tutorial will explain CakePHP find condition & query examples. let's take a look at some CakePHP find conditions. First, a very simple CakePHP find query that retrieves all records from the User model. Example: $this->User->find('all');

    1. CakePHP Query for find user by order:
     

    $users = $this->User->find('all',array('order'=>array('User.created'=>'DESC')));

    2. CakePhp Query for search user   
     

    $users = $this->User->find('all',array('conditions'=>array('username LIKE '=> '%'.$username.'%')));

    3. CakePHP find condition for pending & publish post:
     

       $pending = $this->Post->find('all', array('conditions' => array('Post.status' => '0')));
       $publish = $this->Post->find('all',
                                       array('conditions' => array('Post.status !=' => '0')));

    4. Condition to check email id exist or not in Database for new registration:
     

    $check_email = $this->User->find('first',array('conditions'=>array('User.email_address'=>$user_data['User']['email_address'])));

    5. Condition to check valid login in CakePHP:
     

    $user_data = $this->request->data;
    $username = $user_data['user_name'];
    $password = md5($user_data['password']);
    $check_login = $this->User->find('first',array('conditions'=>array('User.user_name'=>$username,'User.password'=>$password)));

    6. CakePHP find query that uses all the parameters:
     

    $this->Post->find('all',
                      array('conditions'=>array('User.id'=>5),
                            'fields'=>'Post.name',
                            'order'=>'Post.id ASC',
                            'limit'=>20,
                            'recursive'=>0));

    Order: Like sql order conditions. field name must be followed by ASC or DESC.
    fields : which fields should be retrieved in the query
    conditions : array containing conditions as key/value pairs
    Limit: a limit on the number of results returned, like 'select * from posts limit 20'.
    Recursive => 0 : returns the current model & its associated models.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: