更新时间:2023-09-23 12:17:52
首先需要添加ConnectionManager:
First you need to add the ConnectionManager:
use CakeDatasourceConnectionManager;
然后你需要像这样获得你的连接:
Then you need to get your connection like so:
// my_connection is defined in your database config
$conn = ConnectionManager::get('my_connection');
更多信息: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]);
更多信息: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
}
更多信息:http://book.cakephp.org/3.0/en/orm/database-basics.html#executing-fetching-rows