Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to write forward SQL query in cakephp2.x

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 121
    Comment on it

    We might be familiar with the cakephp simple feature which allows us to retrieve and save data in database using find() and save() functions respectively.

    But there is an another way of writing forward SQL queries in cakephp through which we can write complex queries to retrieve and update data in database.we are required to call the query method in cakephp model class and give it the SQL query.

    This can done as :

    $results = $this->Order->query("select * from users order by date");
    

    In the above we write a simple query to select all records from the users table in date wise order.

    We can also write complex queries using the query() method in cakephp as :

     $start_date = $this->params['start_date'];
      $end_date = $this->params['end_date'];
      if (($end_date == '')) $end_date = $start_date;
    
      $foo = $this->User->query("select count(user_name) as num_user from users"
      . " where `date` >= '" . $start_date ."' and `date` <= '" . $end_date . "'");
    

 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: