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)