After a lot of playing around I managed to make it work.
$this->connection->newQuery()->into('failed_logins');
$newIp = $query->func()->inet_aton([$ip]);
$query->insert(['email', 'ip_address', 'sent_email'])->values(
['email' => $email, 'ip_address' => $newIp, 'sent_email' => $sentEmail]
)->execute()->lastInsertId();
Quite complicated and my IDE and PHPStan show me warnings that the function "inet_aton" is not defined.
I would have loved it if in the values()
array I could have just done it like ['ip_address' => "INET_ATON($ip)"]
. Edit: This is not a good idea see comments. But something similar that stays safe can be done with ->bind()
(code snippet below).
Edit: Removed 'literal'
from the code snippet (thanks @ndm)
IDE and Analysis Tool - friendly solution
$this->connection->newQuery()->into('failed_logins');
$query->insert(
[
'email',
'ip_address',
'sent_email',
]
)->values(
[
'email' => $email,
'ip_address' => $query->newExpr("INET_ATON(:ip)"),
'sent_email' => $sentEmail,
]
)->bind(':ip', $ip, 'string')->execute()->lastInsertId();