From 4fbe167fdb1b6a7456e4a612b9d869291bee8e90 Mon Sep 17 00:00:00 2001 From: Matvey Vasenin Date: Tue, 26 May 2015 12:47:07 +0300 Subject: [PATCH 1/3] fix issues #18 --- ActiveRecord.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ActiveRecord.php b/ActiveRecord.php index 845e5a098..407c1bdad 100644 --- a/ActiveRecord.php +++ b/ActiveRecord.php @@ -323,9 +323,11 @@ public static function buildKey($key) } ksort($key); // ensure order is always the same $isNumeric = true; - foreach ($key as $value) { + foreach ($key as &$value) { if (!is_numeric($value)) { $isNumeric = false; + } else { + $value = (string) $value; } } if ($isNumeric) { From 0f37cebb0d9bd88a6019dfc00af2b15b0c636e94 Mon Sep 17 00:00:00 2001 From: Matvey Vasenin Date: Wed, 27 May 2015 13:51:11 +0300 Subject: [PATCH 2/3] test for bug in issue #18 --- tests/ActiveRecordTest.php | 24 ++++++++++++++++++++++ tests/data/ar/OrderWithStringAndINtPK.php | 25 +++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 tests/data/ar/OrderWithStringAndINtPK.php diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index ad9593c59..57ec41055 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -10,6 +10,7 @@ use yiiunit\extensions\redis\data\ar\Item; use yiiunit\extensions\redis\data\ar\OrderItemWithNullFK; use yiiunit\extensions\redis\data\ar\OrderWithNullFK; +use yiiunit\extensions\redis\data\ar\OrderWithStringAndIntPk; use yiiunit\framework\ar\ActiveRecordTestTrait; /** @@ -141,6 +142,10 @@ public function setUp() $orderItem->setAttributes(['order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0], false); $orderItem->save(false); + $order = new OrderWithStringAndIntPk(); + $order->setAttributes(['id' => 1, 'type' => 'cash', 'customer_id' => 1, 'created_at' => 1325282384, 'total' => 110.0], false); + $order->save(false); + } public function testFindEagerViaRelationPreserveOrder() @@ -368,6 +373,25 @@ public function testFilterWhereRecursively() $this->assertEquals(['and', ['id' => 1]], $query->where); } + public function testStringAndIntPkSaving() { + $order = OrderWithStringAndIntPk::findOne([ + 'id' => 1, + 'type' => 'cash', + ]); + + $this->assertEquals(110.0, $order->total); + + $order->total = 200.0; + $order->save(false); + + $order = OrderWithStringAndIntPk::findOne([ + 'id' => 1, + 'type' => 'cash', + ]); + + $this->assertEquals(200.0, $order->total); + } + public function testAutoIncrement() { Customer::getDb()->executeCommand('FLUSHDB'); diff --git a/tests/data/ar/OrderWithStringAndINtPK.php b/tests/data/ar/OrderWithStringAndINtPK.php new file mode 100644 index 000000000..fe1858ffc --- /dev/null +++ b/tests/data/ar/OrderWithStringAndINtPK.php @@ -0,0 +1,25 @@ + Date: Wed, 27 May 2015 13:59:04 +0300 Subject: [PATCH 3/3] typo fix --- tests/ActiveRecordTest.php | 8 ++++---- ...StringAndINtPK.php => OrderWithStringAndIntegerPK.php} | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) rename tests/data/ar/{OrderWithStringAndINtPK.php => OrderWithStringAndIntegerPK.php} (88%) diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 57ec41055..4f8b8002a 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -10,7 +10,7 @@ use yiiunit\extensions\redis\data\ar\Item; use yiiunit\extensions\redis\data\ar\OrderItemWithNullFK; use yiiunit\extensions\redis\data\ar\OrderWithNullFK; -use yiiunit\extensions\redis\data\ar\OrderWithStringAndIntPk; +use yiiunit\extensions\redis\data\ar\OrderWithStringAndIntegerPK; use yiiunit\framework\ar\ActiveRecordTestTrait; /** @@ -142,7 +142,7 @@ public function setUp() $orderItem->setAttributes(['order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0], false); $orderItem->save(false); - $order = new OrderWithStringAndIntPk(); + $order = new OrderWithStringAndIntegerPK(); $order->setAttributes(['id' => 1, 'type' => 'cash', 'customer_id' => 1, 'created_at' => 1325282384, 'total' => 110.0], false); $order->save(false); @@ -374,7 +374,7 @@ public function testFilterWhereRecursively() } public function testStringAndIntPkSaving() { - $order = OrderWithStringAndIntPk::findOne([ + $order = OrderWithStringAndIntegerPK::findOne([ 'id' => 1, 'type' => 'cash', ]); @@ -384,7 +384,7 @@ public function testStringAndIntPkSaving() { $order->total = 200.0; $order->save(false); - $order = OrderWithStringAndIntPk::findOne([ + $order = OrderWithStringAndIntegerPK::findOne([ 'id' => 1, 'type' => 'cash', ]); diff --git a/tests/data/ar/OrderWithStringAndINtPK.php b/tests/data/ar/OrderWithStringAndIntegerPK.php similarity index 88% rename from tests/data/ar/OrderWithStringAndINtPK.php rename to tests/data/ar/OrderWithStringAndIntegerPK.php index fe1858ffc..114571ca6 100644 --- a/tests/data/ar/OrderWithStringAndINtPK.php +++ b/tests/data/ar/OrderWithStringAndIntegerPK.php @@ -11,7 +11,7 @@ * @property integer $created_at * @property string $total */ -class OrderWithStringAndIntPk extends ActiveRecord +class OrderWithStringAndIntegerPK extends ActiveRecord { public static function primaryKey() {