named parameters in request

8ctopus
Member | 2
+
0
-

Hello everyone,

I'm just getting started with Nette database and I must say I really like it, makes coding much faster than using php's vanilla PDO.
Could you please guide me as I can't figure out how to use named parameters in such a query. I have successfully managed to do it with the questions marks, but would prefer named parameters if possible.

use Nette\Database\Connection;

$db = new Connection("mysql:host=...;dbname=...;charset=utf8", ..., ...);

$sql = <<<SQL
    SELECT
        users.date,
        users.transactionId
    FROM
        users
    LEFT OUTER JOIN
        stats
    ON
        stats.transactionId = users.transactionId
    WHERE
        (users.date BETWEEN :minDate AND :maxDate)
SQL;

$query = $db->query($sql, [
    'minDate' => $minDate->format('Y-m-d'),
    'maxDate' => $maxDate->format('Y-m-d'),
]);

Thank you!

Last edited by 8ctopus (2023-03-31 16:01)

8ctopus
Member | 2
+
+1
-

I figured it out:

$db = new Connection("mysql:host=...;dbname=...;charset=utf8", ..., ...);

$sql = <<<SQL
    SELECT
        users.date,
        users.transactionId
    FROM
        users
    LEFT OUTER JOIN
        stats
    ON
        stats.transactionId = users.transactionId
    WHERE
SQL;

$query = $db->query($sql, [
    $db::literal('users.date BETWEEN ? AND ?',
	    $minDate->format('Y-m-d'),
	    $maxDate->format('Y-m-d')
	)
]);