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

[6.x] Fix can't insert float into MySQL with PHP 7.3 #31100

Merged
merged 2 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 0 additions & 18 deletions src/Illuminate/Database/MySqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Database\Query\Processors\MySqlProcessor;
use Illuminate\Database\Schema\Grammars\MySqlGrammar as SchemaGrammar;
use Illuminate\Database\Schema\MySqlBuilder;
use PDO;

class MySqlConnection extends Connection
{
Expand Down Expand Up @@ -64,21 +63,4 @@ protected function getDoctrineDriver()
{
return new DoctrineDriver;
}

/**
* Bind values to their parameters in the given statement.
*
* @param \PDOStatement $statement
* @param array $bindings
* @return void
*/
public function bindValues($statement, $bindings)
{
foreach ($bindings as $key => $value) {
$statement->bindValue(
is_string($key) ? $key : $key + 1, $value,
is_int($value) || is_float($value) ? PDO::PARAM_INT : PDO::PARAM_STR
);
}
}
}
91 changes: 91 additions & 0 deletions tests/Integration/Database/EloquentMySqlConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Illuminate\Database;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\TestCase;

class EloquentMySqlConnectionTest extends TestCase
{
const TABLE = 'player';
const FLOAT_COL = 'float_col';
const JSON_COL = 'json_col';

const FLOAT_VAL = 0.2;

protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');

// Database configuration
$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'username' => 'root',
'password' => '',
'database' => 'forge',
'prefix' => '',
]);
}

protected function setUp(): void
{
parent::setUp();

if (! Schema::hasTable(self::TABLE)) {
Schema::create(self::TABLE, function (Blueprint $table) {
$table->json(self::JSON_COL)->nullable();
$table->float(self::FLOAT_COL)->nullable();
});
}
}

protected function tearDown(): void
{
DB::table(self::TABLE)->truncate();

parent::tearDown();
}

/**
* @dataProvider floatComparisonsDataProvider
*
* @param float $value the value to compare against the JSON value
* @param string $operator the comparison operator to use. e.g. '<', '>', '='
* @param bool $shouldMatch true if the comparison should match, false if not
*/
public function testJsonFloatComparison(float $value, string $operator, bool $shouldMatch): void
{
DB::table(self::TABLE)->insert([self::JSON_COL => '{"rank":'.self::FLOAT_VAL.'}']);
$this->assertSame(
$shouldMatch,
DB::table(self::TABLE)->where(self::JSON_COL.'->rank', $operator, $value)->exists(),
self::JSON_COL.'->rank should '.($shouldMatch ? '' : 'not ')."be $operator $value"
);
}

public function floatComparisonsDataProvider(): array
{
return [
[0.2, '=', true],
[0.2, '>', false],
[0.2, '<', false],
[0.1, '=', false],
[0.1, '<', false],
[0.1, '>', true],
[0.3, '=', false],
[0.3, '<', true],
[0.3, '>', false],
];
}

public function testFloatValueStoredCorrectly(): void
{
DB::table(self::TABLE)->insert([self::FLOAT_COL => self::FLOAT_VAL]);
$this->assertEquals(self::FLOAT_VAL, DB::table(self::TABLE)->value(self::FLOAT_COL));
}
}