Skip to content
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
11 changes: 6 additions & 5 deletions framework/yii/db/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -1485,18 +1485,19 @@ private function bindModels($link, $foreignModel, $primaryModel)
}

/**
* @param array $keys
* @return boolean
* Returns a value indicating whether the given set of attributes represents the primary key for this model
* @param array $keys the set of attributes to check
* @return boolean whether the given set of attributes represents the primary key for this model
*/
private function isPrimaryKey($keys)
public static function isPrimaryKey($keys)
{
$pks = $this->primaryKey();
$pks = static::primaryKey();
foreach ($keys as $key) {
if (!in_array($key, $pks, true)) {
return false;
}
}
return true;
return count($keys) === count($pks);
Copy link
Member Author

Choose a reason for hiding this comment

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

@qiangxue is this change correct?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. The cubrid test fails now. Could you fix it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, will fix it. Seen that CUBRID Schema detection is wrong for composite PK.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed by 6a4ed5a

Copy link
Member

Choose a reason for hiding this comment

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

It seems the test is still failing.

Copy link
Member Author

Choose a reason for hiding this comment

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

}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/framework/db/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,21 @@ public function testBooleanAttribute()
$customers = Customer::find()->where(['status' => false])->all();
$this->assertEquals(1, count($customers));
}

public function testIsPrimaryKey()
{
$this->assertFalse(Customer::isPrimaryKey([]));
$this->assertTrue(Customer::isPrimaryKey(['id']));
$this->assertFalse(Customer::isPrimaryKey(['id', 'name']));
$this->assertFalse(Customer::isPrimaryKey(['name']));
$this->assertFalse(Customer::isPrimaryKey(['name', 'email']));

$this->assertFalse(OrderItem::isPrimaryKey([]));
$this->assertFalse(OrderItem::isPrimaryKey(['order_id']));
$this->assertFalse(OrderItem::isPrimaryKey(['item_id']));
$this->assertFalse(OrderItem::isPrimaryKey(['quantity']));
$this->assertFalse(OrderItem::isPrimaryKey(['quantity', 'subtotal']));
$this->assertTrue(OrderItem::isPrimaryKey(['order_id', 'item_id']));
$this->assertFalse(OrderItem::isPrimaryKey(['order_id', 'item_id', 'quantity']));
}
}