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) { diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index ad9593c59..4f8b8002a 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\OrderWithStringAndIntegerPK; 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 OrderWithStringAndIntegerPK(); + $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 = OrderWithStringAndIntegerPK::findOne([ + 'id' => 1, + 'type' => 'cash', + ]); + + $this->assertEquals(110.0, $order->total); + + $order->total = 200.0; + $order->save(false); + + $order = OrderWithStringAndIntegerPK::findOne([ + 'id' => 1, + 'type' => 'cash', + ]); + + $this->assertEquals(200.0, $order->total); + } + public function testAutoIncrement() { Customer::getDb()->executeCommand('FLUSHDB'); diff --git a/tests/data/ar/OrderWithStringAndIntegerPK.php b/tests/data/ar/OrderWithStringAndIntegerPK.php new file mode 100644 index 000000000..114571ca6 --- /dev/null +++ b/tests/data/ar/OrderWithStringAndIntegerPK.php @@ -0,0 +1,25 @@ +