Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 3 Raw SQL Query

I'm using CakePHP 3, I need to run a raw SQL query on multiple tables. In CakePHP 2, this could be done by using the query() method on any model ( $this->Messages->query("select..") ).

I need the method that allows me to run a SQL query in CakePHP 3. Following is the code snippet I'm using:

$aumTable = TableRegistry::get('Messages');
$sql = "SELECT (SELECT COUNT(*) FROM `messages`) AS `Total_Count`,
        (SELECT COUNT(*) FROM `messages_output`) AS `Total_Output_Count`,
        (SELECT COUNT(*) FROM `messages_output` WHERE `is_success`=1) AS `Total_Successful_Output_Count`,
        (SELECT COUNT(*) FROM `messages_output` WHERE `is_success`=0) AS `Total_Error_Output_Count`,
        (SELECT COUNT(*) FROM `users`) AS `Total_User_Count`;";

// to run this raw SQL query what method should i use? query() doesn't work..
// $result = $aumTable->query($sql); ??
// $result = $aumTable->sql($sql); ??

If you can provide links to CakePHP 3 model documentation where I can find this info, that would be helpful too. I tried searching on google but could only find questions related to CakePHP 2.

like image 806
Ananth Avatar asked Oct 11 '14 08:10

Ananth


People also ask

How can I get data from database in CakePHP?

To view records of database, we first need to get hold of a table using the TableRegistry class. We can fetch the instance out of registry using get() method. The get() method will take the name of the database table as argument. Now, this new instance is used to find records from database using find() method.

How can I get last query in CakePHP?

$last_query = $ this ->ModelName->getLastQuery(); As we have saved last executed query in variable $last_query then use this to print last executed query.


2 Answers

The documentation for this is here: http://book.cakephp.org/3.0/en/orm/database-basics.html#executing-queries

But what's not written there is how to execute it. Because it cost me a while, here is the solution for that:

1.You need to add

use Cake\Datasource\ConnectionManager;

2.init the ConnectionManager (as mentioned above)

$conn = ConnectionManager::get('my_connection');

3.Execute your SQL with something like this

$firstName = $conn->execute('SELECT firstname FROM users WHERE id = 1');
like image 40
Tobias Gaertner Avatar answered Sep 16 '22 11:09

Tobias Gaertner


First you need to add the ConnectionManager:

use Cake\Datasource\ConnectionManager;

Then you need to get your connection like so:

// my_connection is defined in your database config
$conn = ConnectionManager::get('my_connection');

More info: http://book.cakephp.org/3.0/en/orm/database-basics.html#creating-connections-at-runtime

After that you can run a custom query like this:

$stmt = $conn->execute('UPDATE posts SET published = ? WHERE id = ?', [1, 2]);

More info: http://book.cakephp.org/3.0/en/orm/database-basics.html#executing-queries

And then you are ready to fetch the row(s) like this:

// Read one row.
$row = $stmt->fetch('assoc');

// Read all rows.
$rows = $stmt->fetchAll('assoc');

// Read rows through iteration.
foreach ($rows as $row) {
    // Do work
}

More info: http://book.cakephp.org/3.0/en/orm/database-basics.html#executing-fetching-rows

like image 126
Tijme Avatar answered Sep 17 '22 11:09

Tijme