Commit 77840adf by Carsten Brandt

Made ActiveRecord::isPrimaryKey() public

+ added tests and fixed behavior
parent b06caa29
...@@ -1485,18 +1485,19 @@ class ActiveRecord extends Model ...@@ -1485,18 +1485,19 @@ class ActiveRecord extends Model
} }
/** /**
* @param array $keys * Returns a value indicating whether the given set of attributes represents the primary key for this model
* @return boolean * @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) { foreach ($keys as $key) {
if (!in_array($key, $pks, true)) { if (!in_array($key, $pks, true)) {
return false; return false;
} }
} }
return true; return count($keys) === count($pks);
} }
/** /**
......
...@@ -489,4 +489,21 @@ class ActiveRecordTest extends DatabaseTestCase ...@@ -489,4 +489,21 @@ class ActiveRecordTest extends DatabaseTestCase
$customers = Customer::find()->where(['status' => false])->all(); $customers = Customer::find()->where(['status' => false])->all();
$this->assertEquals(1, count($customers)); $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']));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment