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

fix issues #18 #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not forget to unset($value) after cycle end.

if (!is_numeric($value)) {
$isNumeric = false;
} else {
$value = (string) $value;
}
}
if ($isNumeric) {
Expand Down
24 changes: 24 additions & 0 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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');
Expand Down
25 changes: 25 additions & 0 deletions tests/data/ar/OrderWithStringAndIntegerPK.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace yiiunit\extensions\redis\data\ar;

/**
* Class Order
*
* @property integer $id
* @property string $type
* @property integer $customer_id
* @property integer $created_at
* @property string $total
*/
class OrderWithStringAndIntegerPK extends ActiveRecord
{
public static function primaryKey()
{
return ['type', 'id'];
}

public function attributes()
{
return ['id', 'type', 'customer_id', 'created_at', 'total'];
}
}