Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model: update count() for PHP 7.2 #595

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
92187f8
Model: update count() for PHP 7.2
shaneiseminger May 3, 2018
0a55d0e
Model: update count() for PHP 7.2
shaneiseminger May 3, 2018
6c7a0ea
Add Postgres 12 compatibility
shaneiseminger Jul 27, 2020
7494464
Bind PDO values using correct type (fixes ability to use boolean valu…
shaneiseminger Aug 29, 2020
d0b2a63
Add boolean compatibility when loading data
shaneiseminger Aug 31, 2020
c9a8ca6
fix: parse default boolean values correctly
shaneiseminger Sep 29, 2020
4ae80ac
getters and associations should count as being isset() - fixes null c…
shaneiseminger Oct 19, 2020
ee830cd
Add support for Postgres arrays
shaneiseminger Nov 9, 2020
3277380
Remove debugging code
shaneiseminger Nov 9, 2020
da0aabf
Handle encoding of database arrays in SELECT queries correctly
shaneiseminger Nov 9, 2020
b1c54f8
Avoid unnecessarily creating arrays
shaneiseminger Nov 9, 2020
9474b75
Implement alternate method of handling Postgres arrays
shaneiseminger Nov 10, 2020
ed5a887
Implement alternate method of handling Postgres arrays
shaneiseminger Nov 10, 2020
474ece7
7.4 compat fix
shaneiseminger Dec 12, 2020
9c328df
Fix missing obj property
shaneiseminger Jan 31, 2021
0bedb97
Cloned model should be a new record, and should have no PK value
shaneiseminger Feb 24, 2021
15c0d41
Revert "Cloned model should be a new record, and should have no PK va…
shaneiseminger Feb 25, 2021
2e2a4c5
Private methods cannot be final
shaneiseminger Apr 16, 2021
8e90f80
Fix required parameter following optional parameter
shaneiseminger Apr 16, 2021
e19ff51
spl_autoload_register() will always throw now
shaneiseminger Apr 16, 2021
a375c7d
Correct parameters
shaneiseminger Apr 16, 2021
ea3e831
Update tests for PHPUnit 9
shaneiseminger Apr 16, 2021
cc7dcce
$values should default to empty array
shaneiseminger Apr 16, 2021
a06524a
Temp debugging
shaneiseminger Apr 16, 2021
c509af4
Temp debugging
shaneiseminger Apr 16, 2021
d582277
Temp debugging
shaneiseminger Apr 16, 2021
0492dda
workaround weird bug
shaneiseminger Apr 16, 2021
d90e2f0
Always return array
shaneiseminger May 10, 2022
efc44ce
No sequence for UUIDs
shaneiseminger Sep 6, 2023
b736323
Merge branch 'master' of github.com:shaneiseminger/php-activerecord
shaneiseminger Sep 6, 2023
b562a94
No sequence for UUIDs
shaneiseminger Sep 6, 2023
5c1ebb4
Fix passing null
shaneiseminger May 16, 2024
9993dab
No longer can add string + int
shaneiseminger May 16, 2024
114668f
fix datetime issue
luckyastro Jun 12, 2024
d7da45e
Merge pull request #1 from WePostSocial/datetime
luckyastro Jun 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
require __DIR__.'/lib/Cache.php';

if (!defined('PHP_ACTIVERECORD_AUTOLOAD_DISABLE'))
spl_autoload_register('activerecord_autoload',false,PHP_ACTIVERECORD_AUTOLOAD_PREPEND);
spl_autoload_register('activerecord_autoload',true,PHP_ACTIVERECORD_AUTOLOAD_PREPEND);

function activerecord_autoload($class_name)
{
Expand All @@ -40,11 +40,11 @@ function activerecord_autoload($class_name)
foreach ($namespaces as $directory)
$directories[] = $directory;

$root .= DIRECTORY_SEPARATOR . implode($directories, DIRECTORY_SEPARATOR);
$root .= DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $directories);
}

$file = "$root/$class_name.php";

if (file_exists($file))
require_once $file;
}
}
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
"homepage": "http://www.phpactiverecord.org/",
"license": "MIT",
"require": {
"php": ">=5.3.0"
"php": ">=5.3.0",
"rector/rector": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "4.*",
"phpunit/phpunit": "^9",
"pear/pear_exception": "1.0-beta1",
"pear/log": "~1.12"
},
"autoload": {
"files": [ "ActiveRecord.php" ]
},
"autoload-dev": {
"classmap": ["test/helpers"]
}
}
33 changes: 26 additions & 7 deletions lib/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ class Column
const DATETIME = 4;
const DATE = 5;
const TIME = 6;
const BOOLEAN = 7;

/**
* Map a type to an column type.
* @static
* @var array
*/
static $TYPE_MAPPING = array(

'datetime' => self::DATETIME,
'timestamp' => self::DATETIME,
'date' => self::DATE,
Expand All @@ -40,7 +42,11 @@ class Column
'double' => self::DECIMAL,
'numeric' => self::DECIMAL,
'decimal' => self::DECIMAL,
'dec' => self::DECIMAL);
'dec' => self::DECIMAL,

'boolean' => self::BOOLEAN,

);

/**
* The true name of this column.
Expand Down Expand Up @@ -102,6 +108,8 @@ class Column
*/
public $sequence;

public $is_array = false;

/**
* Cast a value to an integer type safely
*
Expand All @@ -128,11 +136,6 @@ public static function castIntegerSafely($value)
elseif (is_numeric($value) && floor($value) != $value)
return (int) $value;

// If adding 0 to a string causes a float conversion,
// we have a number over PHP_INT_MAX
elseif (is_string($value) && is_float($value + 0))
return (string) $value;

// If a float was passed and its greater than PHP_INT_MAX
// (which could be wrong due to floating point precision)
// We'll also check for equal to (>=) in case the precision
Expand All @@ -150,16 +153,31 @@ public static function castIntegerSafely($value)
* @param Connection $connection The Connection this column belongs to
* @return mixed type-casted value
*/
public function cast($value, $connection)
public function cast($value, $connection, bool $process_arrays = true)
{
if ($value === null)
return null;

if ($this->is_array && $process_arrays) {

// Convert database array representation to PHP array
$value_array = is_array($value) ? $value : $connection->database_string_to_array($value);

// Cast each array member according to database type
$self = $this;

return array_map(function ($value) use ($self, $connection) {
return $self->cast($value, $connection, false);
}, $value_array);

}

switch ($this->type)
{
case self::STRING: return (string)$value;
case self::INTEGER: return static::castIntegerSafely($value);
case self::DECIMAL: return (double)$value;
case self::BOOLEAN: return (bool) $value;
case self::DATETIME:
case self::DATE:
if (!$value)
Expand All @@ -179,6 +197,7 @@ public function cast($value, $connection)

return $connection->string_to_datetime($value);
}

return $value;
}

Expand Down
52 changes: 51 additions & 1 deletion lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,32 @@ public function query($sql, &$values=array())

$sth->setFetchMode(PDO::FETCH_ASSOC);

$numeric_keys = false;

foreach ($values as $key => &$value) {
switch (gettype($value)) {
case 'boolean' :
$pdo_type = PDO::PARAM_BOOL;
break;
case 'integer' :
$pdo_type = PDO::PARAM_INT;
break;
case 'NULL' :
$pdo_type = PDO::PARAM_NULL;
break;
default :
$pdo_type = PDO::PARAM_STR;
break;
}
if ($numeric_keys || $key === 0) {
$numeric_keys = true;
++$key;
}
$sth->bindParam($key, $value, $pdo_type);
}

try {
if (!$sth->execute($values))
if (!$sth->execute())
throw new DatabaseException($this);
} catch (PDOException $e) {
throw new DatabaseException($e);
Expand Down Expand Up @@ -499,6 +523,32 @@ public function string_to_datetime($string)
);
}

/**
*
* Converts arrays to string for inserting/updating in database. Necessary because PDO doesn't support arrays directly.
*
* @param array $array The array to serialize
* @return string The serialized array
*/

public function array_to_database_string(array $array)
{
throw new DatabaseException(get_called_class() . ' does not support arrays');
}

/**
*
* Converts arrays to string for inserting/updating in database. Necessary because PDO doesn't support arrays directly.
*
* @param array $array The array to serialize
* @return string The serialized array
*/

public function database_string_to_array(string $value)
{
throw new DatabaseException(get_called_class() . ' does not support arrays');
}

/**
* Adds a limit clause to the SQL query.
*
Expand Down
24 changes: 12 additions & 12 deletions lib/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function attribute_of($model, $attribute_name)
* @param string $format A format string accepted by get_format()
* @return string formatted date and time string
*/
public function format($format=null)
public function format($format=null) : string
{
return parent::format(self::get_format($format));
}
Expand Down Expand Up @@ -123,7 +123,7 @@ public static function createFromFormat($format, $time, $tz = null)
if (!$phpDate)
return false;
// convert to this class using the timestamp
$ourDate = new static(null, $phpDate->getTimezone());
$ourDate = new static('', $phpDate->getTimezone());
$ourDate->setTimestamp($phpDate->getTimestamp());
return $ourDate;
}
Expand Down Expand Up @@ -153,49 +153,49 @@ private function flag_dirty()
$this->model->flag_dirty($this->attribute_name);
}

public function setDate($year, $month, $day)
public function setDate($year, $month, $day) : \DateTime
{
$this->flag_dirty();
return parent::setDate($year, $month, $day);
}

public function setISODate($year, $week , $day = 1)
public function setISODate($year, $week , $day = 1) : \DateTime
{
$this->flag_dirty();
return parent::setISODate($year, $week, $day);
}

public function setTime($hour, $minute, $second = 0, $microseconds = 0)
public function setTime($hour, $minute, $second = 0, $microseconds = 0) : \DateTime
{
$this->flag_dirty();
return parent::setTime($hour, $minute, $second);
}

public function setTimestamp($unixtimestamp)
public function setTimestamp($unixtimestamp) : \DateTime
{
$this->flag_dirty();
return parent::setTimestamp($unixtimestamp);
}

public function setTimezone($timezone)
public function setTimezone($timezone) : \DateTime
{
$this->flag_dirty();
return parent::setTimezone($timezone);
}
public function modify($modify)

public function modify($modify) : \DateTime
{
$this->flag_dirty();
return parent::modify($modify);
}
public function add($interval)

public function add($interval) : \DateTime
{
$this->flag_dirty();
return parent::add($interval);
}

public function sub($interval)
public function sub($interval) : \DateTime
{
$this->flag_dirty();
return parent::sub($interval);
Expand Down
14 changes: 9 additions & 5 deletions lib/Expressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* 'name = :name AND author = :author'
* 'id = IN(:ids)'
* 'id IN(:subselect)'
*
*
* @package ActiveRecord
*/
class Expressions
Expand All @@ -21,6 +21,7 @@ class Expressions
private $expressions;
private $values = array();
private $connection;
private $array_placeholders = [];

public function __construct($connection, $expressions=null /* [, $values ... ] */)
{
Expand Down Expand Up @@ -55,9 +56,10 @@ public function bind($parameter_number, $value)
$this->values[$parameter_number-1] = $value;
}

public function bind_values($values)
public function bind_values($values, $array_placeholders = [])
{
$this->values = $values;
$this->array_placeholders = $array_placeholders;
}

/**
Expand Down Expand Up @@ -90,7 +92,7 @@ public function set_connection($connection)
public function to_s($substitute=false, &$options=null)
{
if (!$options) $options = array();

$values = array_key_exists('values',$options) ? $options['values'] : $this->values;

$ret = "";
Expand Down Expand Up @@ -145,6 +147,7 @@ private function build_sql_from_hash(&$hash, $glue)
private function substitute(&$values, $substitute, $pos, $parameter_index)
{
$value = $values[$parameter_index];
$is_array_placeholder = in_array($parameter_index, $this->array_placeholders);

if (is_array($value))
{
Expand All @@ -165,7 +168,7 @@ private function substitute(&$values, $substitute, $pos, $parameter_index)

return $ret;
}
return join(',',array_fill(0,$value_count,self::ParameterMarker));
return join(',', array_fill(0, $is_array_placeholder ? 1 : $value_count, self::ParameterMarker));
}

if ($substitute)
Expand All @@ -189,4 +192,5 @@ private function quote_string($value)

return "'" . str_replace("'","''",$value) . "'";
}
}
}

Loading