From f3c17594c63b009f6568fc9126a0fc36c5a8c80b Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 7 May 2013 07:18:39 -0400 Subject: [PATCH] Fixes issue #155. --- framework/db/ActiveRecord.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 45c53fb..709d139 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -8,6 +8,7 @@ namespace yii\db; +use yii\base\InvalidConfigException; use yii\base\Model; use yii\base\InvalidParamException; use yii\base\ModelEvent; @@ -112,6 +113,7 @@ class ActiveRecord extends Model * @return ActiveQuery|ActiveRecord|null When `$q` is null, a new [[ActiveQuery]] instance * is returned; when `$q` is a scalar or an array, an ActiveRecord object matching it will be * returned (null will be returned if there is no matching). + * @throws InvalidConfigException if the AR class does not have a primary key * @see createQuery() */ public static function find($q = null) @@ -122,7 +124,11 @@ class ActiveRecord extends Model } elseif ($q !== null) { // query by primary key $primaryKey = static::primaryKey(); - return $query->where(array($primaryKey[0] => $q))->one(); + if (isset($primaryKey[0])) { + return $query->where(array($primaryKey[0] => $q))->one(); + } else { + throw new InvalidConfigException(get_called_class() . ' must have a primary key.'); + } } return $query; }