Skip to content

Commit

Permalink
Fix #324: conversion int/bool for PostgreSQL and other strict DB
Browse files Browse the repository at this point in the history
This patch doesn't modify the database schema and, instead, forcibly converts
booleans to integers.

Another way of fixing #324 would be to use booleans at the database level : the
code change is bigger but, since MySQL & MariaDB would implictly convert to
tinyint and SQLite isn't typed, the impact on existing deployments may still be
acceptable.
  • Loading branch information
xlii-chl authored and MGatner committed Jul 12, 2022
1 parent b0b3bb6 commit 5fa9090
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ public function setPassword(string $password)
$this->attributes['reset_expires'] = null;
}

/**
* Explicitly convert false and true to 0 and 1
*
* Some BDD (PostgreSQL for example) can be picky about data types and
* if 'active' or 'force_pass_reset' are set to (bool)true/false, the method
* CodeIgniter\Database\Postgre\Connection::escape() will translate it
* to a literal TRUE/FALSE. Since the database fields are defined as integer,
* the BDD will throw an error about mismatched type.
*
* @param bool|int $active
*/
public function setActive($active)
{
$this->attributes['active'] = $active ? 1 : 0;
}

/**
* Explicitly convert false and true to 0 and 1
*
* @see setActive() Explanation about strict typing at database level
*
* @param bool|int $force_pass_reset
*/
public function setForcePassReset($force_pass_reset)
{
$this->attributes['force_pass_reset'] = $force_pass_reset ? 1 : 0;
}

/**
* Force a user to reset their password on next page refresh
* or login. Checked in the LocalAuthenticator's check() method.
Expand Down

0 comments on commit 5fa9090

Please sign in to comment.