I would like to learn how to create an SQL query with CakePHP

My query

SELECT SUM(amount)
FROM payments
INNER JOIN accounting_payments ON accounting_payments.payment_id = payments.id
WHERE accounting_payments.accounting_id = 1 AND payments.type = 1

My first try

$query = $this->Accounting->Payments->find();
$query->select(['sum' => $query->func()->sum('amount')])->where(['type' => 1]);
print_r($query);

I want to get the result of my query. How do I do that?

My solution

$connection = ConnectionManager::get('default');
$query = "
    SELECT SUM(amount)
    FROM payments
    INNER JOIN accounting_payments ON accounting_payments.payment_id = payments.id
    WHERE accounting_payments.accounting_id = 1 AND payments.type = 1
";
$result = $connection->execute($query)->fetchAll();
return $result[0][0];