diff --git a/apps/advanced/common/models/User.php b/apps/advanced/common/models/User.php index 9cac532..ad7d48d 100644 --- a/apps/advanced/common/models/User.php +++ b/apps/advanced/common/models/User.php @@ -69,7 +69,7 @@ class User extends ActiveRecord implements IdentityInterface */ public static function findIdentity($id) { - return static::find($id); + return static::findOne($id); } /** @@ -88,7 +88,7 @@ class User extends ActiveRecord implements IdentityInterface */ public static function findByUsername($username) { - return static::find(['username' => $username, 'status' => self::STATUS_ACTIVE]); + return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); } /** @@ -107,7 +107,7 @@ class User extends ActiveRecord implements IdentityInterface return null; } - return static::find([ + return static::findOne([ 'password_reset_token' => $token, 'status' => self::STATUS_ACTIVE, ]); diff --git a/apps/advanced/frontend/models/PasswordResetRequestForm.php b/apps/advanced/frontend/models/PasswordResetRequestForm.php index 664bb04..2fa59fe 100644 --- a/apps/advanced/frontend/models/PasswordResetRequestForm.php +++ b/apps/advanced/frontend/models/PasswordResetRequestForm.php @@ -36,7 +36,7 @@ class PasswordResetRequestForm extends Model public function sendEmail() { /** @var User $user */ - $user = User::find([ + $user = User::findOne([ 'status' => User::STATUS_ACTIVE, 'email' => $this->email, ]); diff --git a/apps/advanced/frontend/tests/unit/models/PasswordResetRequestFormTest.php b/apps/advanced/frontend/tests/unit/models/PasswordResetRequestFormTest.php index 91c52ff..ca445be 100644 --- a/apps/advanced/frontend/tests/unit/models/PasswordResetRequestFormTest.php +++ b/apps/advanced/frontend/tests/unit/models/PasswordResetRequestFormTest.php @@ -47,7 +47,7 @@ class PasswordResetRequestFormTest extends DbTestCase { $model = new PasswordResetRequestForm(); $model->email = $this->user[0]['email']; - $user = User::find(['password_reset_token' => $this->user[0]['password_reset_token']]); + $user = User::findOne(['password_reset_token' => $this->user[0]['password_reset_token']]); expect('email sent', $model->sendEmail())->true(); expect('user has valid token', $user->password_reset_token)->notNull(); diff --git a/docs/guide/active-record.md b/docs/guide/active-record.md index 4e9680f..ac0fe91 100644 --- a/docs/guide/active-record.md +++ b/docs/guide/active-record.md @@ -169,17 +169,17 @@ $customers = Customer::findBySql($sql)->all(); use meaningful constant names rather than hardcoded strings or numbers in your code. -The `find()` method also supports the following shortcut usage which allows you to retrieve an Active Record -instance based on a primary key value or a set of column values. The main difference here is that instead of -returning a [[yii\db\ActiveQuery]] instance, the method takes the column value(s) and returns an Active Record -instance directly without the need to call `one()`. +There is a shortcut method `findOne()` that allows you to retrieve an Active Record instance based on a primary +key value or a set of column values. The main difference here is that instead of returning a [[yii\db\ActiveQuery]] +instance, the method takes the column value(s) and returns an Active Record instance directly without the need +to call `one()`. ```php // to return a single customer whose ID is 1: -$customer = Customer::find(1); +$customer = Customer::findOne(1); // to return an *active* customer whose ID is 1: -$customer = Customer::find([ +$customer = Customer::findOne([ 'id' => 1, 'status' => Customer::STATUS_ACTIVE, ]); @@ -252,12 +252,12 @@ $customer->email = 'james@example.com'; $customer->save(); // equivalent to $customer->insert(); // to update an existing customer record -$customer = Customer::find($id); +$customer = Customer::findOne($id); $customer->email = 'james@example.com'; $customer->save(); // equivalent to $customer->update(); // to delete an existing customer record -$customer = Customer::find($id); +$customer = Customer::findOne($id); $customer->delete(); // to increment the age of ALL customers by 1 @@ -267,8 +267,8 @@ Customer::updateAllCounters(['age' => 1]); > Info: The `save()` method will call either `insert()` or `update()`, depending on whether the Active Record instance is new or not (internally it will check the value of [[yii\db\ActiveRecord::isNewRecord]]). If an Active Record is instantiated via the `new` operator, calling `save()` will - insert a row in the table; if an Active Record is obtained by `find()`, calling `save()` will - update the corresponding row in the table. + insert a row in the table; calling `save()` on active record fetched from database will update the corresponding + row in the table. ### Data Input and Validation @@ -291,7 +291,7 @@ if ($model->load(Yii::$app->request->post()) && $model->save()) { } // updating a record whose primary key is $id -$model = Customer::find($id); +$model = Customer::findOne($id); if ($model === null) { throw new NotFoundHttpException; } @@ -399,7 +399,7 @@ that is defined by the corresponding getter method: ```php // get the orders of a customer -$customer = Customer::find(1); +$customer = Customer::findOne(1); $orders = $customer->orders; // $orders is an array of Order objects ``` @@ -501,7 +501,7 @@ if you access the same related objects again. We call this *lazy loading*. For e ```php // SQL executed: SELECT * FROM customer WHERE id=1 -$customer = Customer::find(1); +$customer = Customer::findOne(1); // SQL executed: SELECT * FROM order WHERE customer_id=1 $orders = $customer->orders; // no SQL executed @@ -559,7 +559,7 @@ Sometimes, you may want to customize the relational queries on the fly. This can done for both lazy loading and eager loading. For example, ```php -$customer = Customer::find(1); +$customer = Customer::findOne(1); // lazy loading: SELECT * FROM order WHERE customer_id=1 AND subtotal>100 $orders = $customer->getOrders()->where('subtotal>100')->all(); @@ -605,7 +605,7 @@ the `customer` of an order will trigger another SQL execution: ```php // SELECT * FROM customer WHERE id=1 -$customer = Customer::find(1); +$customer = Customer::findOne(1); // echoes "not equal" // SELECT * FROM order WHERE customer_id=1 // SELECT * FROM customer WHERE id=1 @@ -634,7 +634,7 @@ Now if we execute the same query as shown above, we would get: ```php // SELECT * FROM customer WHERE id=1 -$customer = Customer::find(1); +$customer = Customer::findOne(1); // echoes "equal" // SELECT * FROM order WHERE customer_id=1 if ($customer->orders[0]->customer === $customer) { @@ -762,7 +762,7 @@ in the WHERE part of the corresponding SQL statement, because there is no JOIN q ```php // SELECT * FROM user WHERE id=10 -$user = User::find(10); +$user = User::findOne(10); // SELECT * FROM item WHERE owner_id=10 AND category_id=1 $books = $user->books; ``` @@ -781,7 +781,7 @@ For example, given a customer and a new order, we can use the following code to order owned by the customer: ```php -$customer = Customer::find(1); +$customer = Customer::findOne(1); $order = new Order(); $order->subtotal = 100; $customer->link('orders', $order); @@ -828,7 +828,7 @@ Important points are: 2. A method should be `public` and should return `$this` in order to allow method chaining. It may accept parameters. 3. Check [[yii\db\ActiveQuery]] methods that are very useful for modifying query conditions. -Second, override [[yii\db\ActiveRecord::createQuery()]] to use the custom query class instead of the regular [[yii\db\ActiveQuery|ActiveQuery]]. +Second, override [[yii\db\ActiveRecord::find()]] to use the custom query class instead of the regular [[yii\db\ActiveQuery|ActiveQuery]]. For the example above, you need to write the following code: ```php @@ -838,7 +838,11 @@ use yii\db\ActiveRecord; class Comment extends ActiveRecord { - public static function createQuery() + /** + * @inheritdoc + * @return CommentQuery + */ + public static function find() { return new CommentQuery(get_called_class()); } @@ -875,43 +879,15 @@ $posts = Post::find()->with([ ])->all(); ``` - -### Making it IDE-friendly - -In order to make most modern IDE autocomplete happy you need to override return types for some methods of both model -and query like the following: - -```php -/** - * @method \app\models\CommentQuery|static|null find($q = null) static - * @method \app\models\CommentQuery findBySql($sql, $params = []) static - */ -class Comment extends ActiveRecord -{ - // ... -} -``` - -```php -/** - * @method \app\models\Comment|array|null one($db = null) - * @method \app\models\Comment[]|array all($db = null) - */ -class CommentQuery extends ActiveQuery -{ - // ... -} -``` - ### Default Scope If you used Yii 1.1 before, you may know a concept called *default scope*. A default scope is a scope that -applies to ALL queries. You can define a default scope easily by overriding [[yii\db\ActiveRecord::createQuery()]]. For example, +applies to ALL queries. You can define a default scope easily by overriding [[yii\db\ActiveRecord::find()]]. For example, ```php -public static function createQuery() +public static function find() { - return parent::createQuery()->where(['deleted' => false]); + return parent::find()->where(['deleted' => false]); } ``` diff --git a/docs/guide/authentication.md b/docs/guide/authentication.md index df2ea7b..7c1383c 100644 --- a/docs/guide/authentication.md +++ b/docs/guide/authentication.md @@ -21,7 +21,7 @@ class User extends ActiveRecord implements IdentityInterface */ public static function findIdentity($id) { - return static::find($id); + return static::findOne($id); } /** @@ -32,7 +32,7 @@ class User extends ActiveRecord implements IdentityInterface */ public static function findIdentityByAccessToken($token) { - return static::find(['access_token' => $token]); + return static::findOne(['access_token' => $token]); } /** diff --git a/docs/guide/authorization.md b/docs/guide/authorization.md index 91c943c..093305c 100644 --- a/docs/guide/authorization.md +++ b/docs/guide/authorization.md @@ -266,7 +266,7 @@ simple checks could be used instead. For example such code that uses RBAC: ```php public function editArticle($id) { - $article = Article::find($id); + $article = Article::findOne($id); if (!$article) { throw new NotFoundHttpException; } @@ -282,7 +282,7 @@ can be replaced with simpler code that doesn't use RBAC: ```php public function editArticle($id) { - $article = Article::find(['id' => $id, 'author_id' => \Yii::$app->user->id]); + $article = Article::findOne(['id' => $id, 'author_id' => \Yii::$app->user->id]); if (!$article) { throw new NotFoundHttpException; } diff --git a/docs/guide/controller.md b/docs/guide/controller.md index 12ac39d..f098ad6 100644 --- a/docs/guide/controller.md +++ b/docs/guide/controller.md @@ -91,7 +91,7 @@ class BlogController extends Controller { public function actionView($id, $version = null) { - $post = Post::find($id); + $post = Post::findOne($id); $text = $post->text; if ($version) { @@ -125,7 +125,7 @@ class BlogController extends Controller { public function actionUpdate($id) { - $post = Post::find($id); + $post = Post::findOne($id); if (!$post) { throw new NotFoundHttpException(); } diff --git a/docs/guide/model.md b/docs/guide/model.md index 7608acf..bb2a444 100644 --- a/docs/guide/model.md +++ b/docs/guide/model.md @@ -295,7 +295,7 @@ The following code will return *all* attributes in the `$post` model as an array of name-value pairs. ```php -$post = Post::find(42); +$post = Post::findOne(42); if ($post) { $attributes = $post->attributes; var_dump($attributes); @@ -356,7 +356,7 @@ class User extends ActiveRecord For the code above mass assignment will be allowed strictly according to `scenarios()`: ```php -$user = User::find(42); +$user = User::findOne(42); $data = ['password' => '123']; $user->attributes = $data; print_r($user->attributes); @@ -365,7 +365,7 @@ print_r($user->attributes); Will give you empty array because there's no default scenario defined in our `scenarios()`. ```php -$user = User::find(42); +$user = User::findOne(42); $user->scenario = 'signup'; $data = [ 'username' => 'samdark', @@ -406,7 +406,7 @@ class User extends ActiveRecord The code above assumes default scenario so mass assignment will be available for all fields with `rules` defined: ```php -$user = User::find(42); +$user = User::findOne(42); $data = [ 'username' => 'samdark', 'first_name' => 'Alexander', @@ -453,7 +453,7 @@ class User extends ActiveRecord Mass assignment is still available by default: ```php -$user = User::find(42); +$user = User::findOne(42); $data = [ 'username' => 'samdark', 'first_name' => 'Alexander', diff --git a/docs/guide/rest.md b/docs/guide/rest.md index 9165dab..c01ffcb 100644 --- a/docs/guide/rest.md +++ b/docs/guide/rest.md @@ -663,7 +663,7 @@ class User extends ActiveRecord implements IdentityInterface { public static function findIdentityByAccessToken($token) { - return static::find(['access_token' => $token]); + return static::findOne(['access_token' => $token]); } } ``` diff --git a/docs/guide/upgrade-from-v1.md b/docs/guide/upgrade-from-v1.md index 71ceeeb..d9013f7 100644 --- a/docs/guide/upgrade-from-v1.md +++ b/docs/guide/upgrade-from-v1.md @@ -454,7 +454,7 @@ $customers = Customer::find() ->orderBy('id') ->all(); // return the customer whose PK is 1 -$customer = Customer::find(1); +$customer = Customer::findOne(1); ``` diff --git a/extensions/elasticsearch/ActiveRecord.php b/extensions/elasticsearch/ActiveRecord.php index b4c7cf6..56fb1f6 100644 --- a/extensions/elasticsearch/ActiveRecord.php +++ b/extensions/elasticsearch/ActiveRecord.php @@ -65,19 +65,21 @@ class ActiveRecord extends BaseActiveRecord /** * @inheritdoc */ - public static function find($q = null) + public static function find() { - $query = static::createQuery(); - $args = func_get_args(); - if (empty($args)) { - return $query; - } + return new ActiveQuery(get_called_class()); + } - $q = reset($args); - if (is_array($q)) { - return $query->andWhere($q)->one(); + /** + * @inheritdoc + */ + public static function findOne($condtion) + { + $query = static::find(); + if (is_array($condtion)) { + return $query->andWhere($condtion)->one(); } else { - return static::get($q); + return static::get($condtion); } } @@ -145,33 +147,6 @@ class ActiveRecord extends BaseActiveRecord // TODO add percolate functionality http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-percolate.html - /** - * Creates an [[ActiveQuery]] instance. - * - * This method is called by [[find()]] to start a SELECT query but also - * by [[hasOne()]] and [[hasMany()]] to create a relational query. - * You may override this method to return a customized query (e.g. `CustomerQuery` specified - * written for querying `Customer` purpose.) - * - * You may also define default conditions that should apply to all queries unless overridden: - * - * ```php - * public static function createQuery() - * { - * return parent::createQuery()->where(['deleted' => false]); - * } - * ``` - * - * Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the - * default condition. Using [[Query::where()]] will override the default condition. - * - * @return ActiveQuery the newly created [[ActiveQuery]] instance. - */ - public static function createQuery() - { - return new ActiveQuery(get_called_class()); - } - // TODO implement copy and move as pk change is not possible /** diff --git a/extensions/gii/generators/crud/default/controller.php b/extensions/gii/generators/crud/default/controller.php index 0a00eb4..9bfef66 100644 --- a/extensions/gii/generators/crud/default/controller.php +++ b/extensions/gii/generators/crud/default/controller.php @@ -150,7 +150,7 @@ if (count($pks) === 1) { $condition = '[' . implode(', ', $condition) . ']'; } ?> - if (($model = <?= $modelClass ?>::find(<?= $condition ?>)) !== null) { + if (($model = <?= $modelClass ?>::findOne(<?= $condition ?>)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); diff --git a/extensions/mongodb/ActiveRecord.php b/extensions/mongodb/ActiveRecord.php index a64400e..d9ac254 100644 --- a/extensions/mongodb/ActiveRecord.php +++ b/extensions/mongodb/ActiveRecord.php @@ -92,28 +92,9 @@ abstract class ActiveRecord extends BaseActiveRecord } /** - * Creates an [[ActiveQuery]] instance. - * - * This method is called by [[find()]] to start a SELECT query but also - * by [[hasOne()]] and [[hasMany()]] to create a relational query. - * You may override this method to return a customized query (e.g. `CustomerQuery` specified - * written for querying `Customer` purpose.) - * - * You may also define default conditions that should apply to all queries unless overridden: - * - * ```php - * public static function createQuery() - * { - * return parent::createQuery()->where(['deleted' => false]); - * } - * ``` - * - * Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the - * default condition. Using [[Query::where()]] will override the default condition. - * - * @return ActiveQuery the newly created [[ActiveQuery]] instance. + * @inheritdoc */ - public static function createQuery() + public static function find() { return new ActiveQuery(get_called_class()); } diff --git a/extensions/mongodb/file/ActiveRecord.php b/extensions/mongodb/file/ActiveRecord.php index f2e1600..bcab138 100644 --- a/extensions/mongodb/file/ActiveRecord.php +++ b/extensions/mongodb/file/ActiveRecord.php @@ -45,28 +45,9 @@ use yii\web\UploadedFile; abstract class ActiveRecord extends \yii\mongodb\ActiveRecord { /** - * Creates an [[ActiveQuery]] instance. - * - * This method is called by [[find()]] to start a SELECT query but also - * by [[hasOne()]] and [[hasMany()]] to create a relational query. - * You may override this method to return a customized query (e.g. `CustomerQuery` specified - * written for querying `Customer` purpose.) - * - * You may also define default conditions that should apply to all queries unless overridden: - * - * ```php - * public static function createQuery() - * { - * return parent::createQuery()->where(['deleted' => false]); - * } - * ``` - * - * Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the - * default condition. Using [[Query::where()]] will override the default condition. - * - * @return ActiveQuery the newly created [[ActiveQuery]] instance. + * @inheritdoc */ - public static function createQuery() + public static function find() { return new ActiveQuery(get_called_class()); } diff --git a/extensions/redis/ActiveRecord.php b/extensions/redis/ActiveRecord.php index 8bb22d2..56b0637 100644 --- a/extensions/redis/ActiveRecord.php +++ b/extensions/redis/ActiveRecord.php @@ -49,28 +49,9 @@ class ActiveRecord extends BaseActiveRecord } /** - * Creates an [[ActiveQuery]] instance. - * - * This method is called by [[find()]] to start a SELECT query but also - * by [[hasOne()]] and [[hasMany()]] to create a relational query. - * You may override this method to return a customized query (e.g. `CustomerQuery` specified - * written for querying `Customer` purpose.) - * - * You may also define default conditions that should apply to all queries unless overridden: - * - * ```php - * public static function createQuery() - * { - * return parent::createQuery()->where(['deleted' => false]); - * } - * ``` - * - * Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the - * default condition. Using [[Query::where()]] will override the default condition. - * - * @return ActiveQuery the newly created [[ActiveQuery]] instance. + * @inheritdoc */ - public static function createQuery() + public static function find() { return new ActiveQuery(get_called_class()); } @@ -274,7 +255,7 @@ class ActiveRecord extends BaseActiveRecord private static function fetchPks($condition) { - $query = static::createQuery(); + $query = static::find(); $query->where($condition); $records = $query->asArray()->all(); // TODO limit fetched columns to pk $primaryKey = static::primaryKey(); diff --git a/extensions/sphinx/ActiveRecord.php b/extensions/sphinx/ActiveRecord.php index 290fe19..7786a36 100644 --- a/extensions/sphinx/ActiveRecord.php +++ b/extensions/sphinx/ActiveRecord.php @@ -85,7 +85,7 @@ abstract class ActiveRecord extends BaseActiveRecord */ public static function findBySql($sql, $params = []) { - $query = static::createQuery(); + $query = static::find(); $query->sql = $sql; return $query->params($params); @@ -136,28 +136,9 @@ abstract class ActiveRecord extends BaseActiveRecord } /** - * Creates an [[ActiveQuery]] instance. - * - * This method is called by [[find()]], [[findBySql()]] to start a SELECT query but also - * by [[hasOne()]] and [[hasMany()]] to create a relational query. - * You may override this method to return a customized query (e.g. `CustomerQuery` specified - * written for querying `Customer` purpose.) - * - * You may also define default conditions that should apply to all queries unless overridden: - * - * ```php - * public static function createQuery() - * { - * return parent::createQuery()->where(['deleted' => false]); - * } - * ``` - * - * Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the - * default condition. Using [[Query::where()]] will override the default condition. - * - * @return ActiveQuery the newly created [[ActiveQuery]] instance. + * @inheritdoc */ - public static function createQuery() + public static function find() { return new ActiveQuery(get_called_class()); } diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 26c4a8a..a4da15a 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -143,7 +143,7 @@ class ActiveRecord extends BaseActiveRecord */ public static function findBySql($sql, $params = []) { - $query = static::createQuery(); + $query = static::find(); $query->sql = $sql; return $query->params($params); @@ -224,28 +224,9 @@ class ActiveRecord extends BaseActiveRecord } /** - * Creates an [[ActiveQuery]] instance. - * - * This method is called by [[find()]], [[findBySql()]] to start a SELECT query but also - * by [[hasOne()]] and [[hasMany()]] to create a relational query. - * You may override this method to return a customized query (e.g. `CustomerQuery` specified - * written for querying `Customer` purpose.) - * - * You may also define default conditions that should apply to all queries unless overridden: - * - * ```php - * public static function createQuery() - * { - * return parent::createQuery()->where(['deleted' => false]); - * } - * ``` - * - * Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the - * default condition. Using [[Query::where()]] will override the default condition. - * - * @return ActiveQuery the newly created [[ActiveQuery]] instance. + * @inheritdoc */ - public static function createQuery() + public static function find() { return new ActiveQuery(get_called_class()); } @@ -479,7 +460,7 @@ class ActiveRecord extends BaseActiveRecord * For example, to update a customer record: * * ~~~ - * $customer = Customer::find($id); + * $customer = Customer::findOne($id); * $customer->name = $name; * $customer->email = $email; * $customer->update(); diff --git a/framework/db/ActiveRecordInterface.php b/framework/db/ActiveRecordInterface.php index 8418581..6fe7528 100644 --- a/framework/db/ActiveRecordInterface.php +++ b/framework/db/ActiveRecordInterface.php @@ -72,7 +72,7 @@ interface ActiveRecordInterface /** * Returns the old primary key value(s). * This refers to the primary key value that is populated into the record - * after executing a find method (e.g. find(), findAll()). + * after executing a find method (e.g. find(), findOne()). * The value remains unchanged even if the primary key attribute is manually assigned with a different value. * @param boolean $asArray whether to return the primary key value as an array. If true, * the return value will be an array with column name as key and column value as value. @@ -111,7 +111,32 @@ interface ActiveRecordInterface * ->all(); * ``` * - * This method can also take a parameter which can be: + * This method is also called by [[BaseActiveRecord::hasOne()]] and [[BaseActiveRecord::hasMany()]] to + * create a relational query. + * + * You may override this method to return a customized query (e.g. `CustomerQuery` specified + * written for querying `Customer` purpose.) + * + * You may also define default conditions that should apply to all queries unless overridden: + * + * ```php + * public static function find() + * { + * return parent::find()->where(['deleted' => false]); + * } + * ``` + * + * Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the + * default condition. Using [[Query::where()]] will override the default condition. + * + * @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance. + */ + public static function find(); + + /** + * Returns a single active record model instance given a primary key or an array of column values. + * + * The method accepts: * * - a scalar value (integer or string): query by a single primary key value and return the * corresponding record (or null if not found). @@ -123,50 +148,22 @@ interface ActiveRecordInterface * * ```php * // find a single customer whose primary key value is 10 - * $customer = Customer::find(10); + * $customer = Customer::findOne(10); * * // the above code is equivalent to: * $customer = Customer::find()->where(['id' => 10])->one(); * * // find a single customer whose age is 30 and whose status is 1 - * $customer = Customer::find(['age' => 30, 'status' => 1]); + * $customer = Customer::findOne(['age' => 30, 'status' => 1]); * * // the above code is equivalent to: * $customer = Customer::find()->where(['age' => 30, 'status' => 1])->one(); * ``` * - * @return ActiveQueryInterface|static|null When this method receives no parameter, a new [[ActiveQuery]] instance - * will be returned; Otherwise, the parameter will be treated as a primary key value or a set of column - * values, and an ActiveRecord object matching it will be returned (null will be returned if there is no matching). - * @see createQuery() - */ - public static function find(); - - /** - * Creates an [[ActiveQueryInterface|ActiveQuery]] instance. - * - * This method is called by [[find()]] to start a SELECT query but also - * by [[BaseActiveRecord::hasOne()]] and [[BaseActiveRecord::hasMany()]] to - * create a relational query. - * - * You may override this method to return a customized query (e.g. `CustomerQuery` specified - * written for querying `Customer` purpose.) - * - * You may also define default conditions that should apply to all queries unless overridden: - * - * ```php - * public static function createQuery() - * { - * return parent::createQuery()->where(['deleted' => false]); - * } - * ``` - * - * Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the - * default condition. Using [[Query::where()]] will override the default condition. - * - * @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance. + * @param mixed $condition primary key value or a set of column values + * @return static ActiveRecord instance or null if nothing matched */ - public static function createQuery(); + public static function findOne($condition); /** * Updates records using the provided attribute values and conditions. diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index e041086..645dd59 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -94,22 +94,16 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface /** * @inheritdoc */ - public static function find() + public static function findOne($condition) { - $query = static::createQuery(); - $args = func_get_args(); - if (empty($args)) { - return $query; - } - - $q = reset($args); - if (is_array($q)) { - return $query->andWhere($q)->one(); + $query = static::find(); + if (is_array($condition)) { + return $query->andWhere($condition)->one(); } else { // query by primary key $primaryKey = static::primaryKey(); if (isset($primaryKey[0])) { - return $query->andWhere([$primaryKey[0] => $q])->one(); + return $query->andWhere([$primaryKey[0] => $condition])->one(); } else { throw new InvalidConfigException(get_called_class() . ' must have a primary key.'); } @@ -310,7 +304,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface { /** @var ActiveRecordInterface $class */ /** @var ActiveQuery $query */ - $query = $class::createQuery(); + $query = $class::find(); $query->primaryModel = $this; $query->link = $link; $query->multiple = false; @@ -351,7 +345,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface { /** @var ActiveRecordInterface $class */ /** @var ActiveQuery $query */ - $query = $class::createQuery(); + $query = $class::find(); $query->primaryModel = $this; $query->link = $link; $query->multiple = true; @@ -540,7 +534,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface * For example, to save a customer record: * * ~~~ - * $customer = new Customer; // or $customer = Customer::find($id); + * $customer = new Customer; // or $customer = Customer::findOne($id); * $customer->name = $name; * $customer->email = $email; * $customer->save(); @@ -584,7 +578,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface * For example, to update a customer record: * * ~~~ - * $customer = Customer::find($id); + * $customer = Customer::findOne($id); * $customer->name = $name; * $customer->email = $email; * $customer->update(); @@ -695,7 +689,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface * An example usage is as follows: * * ~~~ - * $post = Post::find($id); + * $post = Post::findOne($id); * $post->updateCounters(['view_count' => 1]); * ~~~ * @@ -891,7 +885,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface */ public function refresh() { - $record = $this->find($this->getPrimaryKey(true)); + $record = $this->findOne($this->getPrimaryKey(true)); if ($record === null) { return false; } @@ -950,7 +944,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface /** * Returns the old primary key value(s). * This refers to the primary key value that is populated into the record - * after executing a find method (e.g. find(), findAll()). + * after executing a find method (e.g. one(), findOne()). * The value remains unchanged even if the primary key attribute is manually assigned with a different value. * @param boolean $asArray whether to return the primary key value as an array. If true, * the return value will be an array with column name as key and column value as value. diff --git a/framework/filters/auth/HttpBasicAuth.php b/framework/filters/auth/HttpBasicAuth.php index 00b06b0..565112c 100644 --- a/framework/filters/auth/HttpBasicAuth.php +++ b/framework/filters/auth/HttpBasicAuth.php @@ -44,7 +44,7 @@ class HttpBasicAuth extends AuthMethod * * ```php * function ($username, $password) { - * return \app\models\User::find([ + * return \app\models\User::findOne([ * 'username' => $username, * 'password' => $password, * ]); diff --git a/framework/rest/Action.php b/framework/rest/Action.php index d0e9998..e906183 100644 --- a/framework/rest/Action.php +++ b/framework/rest/Action.php @@ -90,10 +90,10 @@ class Action extends \yii\base\Action if (count($keys) > 1) { $values = explode(',', $id); if (count($keys) === count($values)) { - $model = $modelClass::find(array_combine($keys, $values)); + $model = $modelClass::findOne(array_combine($keys, $values)); } } elseif ($id !== null) { - $model = $modelClass::find($id); + $model = $modelClass::findOne($id); } if (isset($model)) { diff --git a/framework/test/BaseActiveFixture.php b/framework/test/BaseActiveFixture.php index a6c11c7..3625a90 100644 --- a/framework/test/BaseActiveFixture.php +++ b/framework/test/BaseActiveFixture.php @@ -68,7 +68,7 @@ abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate $keys[$key] = isset($row[$key]) ? $row[$key] : null; } - return $this->_models[$name] = $modelClass::find($keys); + return $this->_models[$name] = $modelClass::findOne($keys); } /** diff --git a/framework/web/IdentityInterface.php b/framework/web/IdentityInterface.php index ee32bcb..fabebbe 100644 --- a/framework/web/IdentityInterface.php +++ b/framework/web/IdentityInterface.php @@ -18,7 +18,7 @@ namespace yii\web; * { * public static function findIdentity($id) * { - * return static::find($id); + * return static::findOne($id); * } * * public function getId() diff --git a/tests/unit/data/ar/Customer.php b/tests/unit/data/ar/Customer.php index 855a4b8..6153984 100644 --- a/tests/unit/data/ar/Customer.php +++ b/tests/unit/data/ar/Customer.php @@ -13,7 +13,6 @@ use yiiunit\framework\db\ActiveRecordTest; * @property string $address * @property integer $status * - * @method CustomerQuery|Customer|null find($q = null) static * @method CustomerQuery findBySql($sql, $params = []) static */ class Customer extends ActiveRecord @@ -62,7 +61,11 @@ class Customer extends ActiveRecord parent::afterSave($insert); } - public static function createQuery() + /** + * @inheritdoc + * @return CustomerQuery + */ + public static function find() { return new CustomerQuery(get_called_class()); } diff --git a/tests/unit/data/ar/elasticsearch/Customer.php b/tests/unit/data/ar/elasticsearch/Customer.php index 61c1856..ac75f5f 100644 --- a/tests/unit/data/ar/elasticsearch/Customer.php +++ b/tests/unit/data/ar/elasticsearch/Customer.php @@ -64,7 +64,11 @@ class Customer extends ActiveRecord } - public static function createQuery() + /** + * @inheritdoc + * @return CustomerQuery + */ + public static function find() { return new CustomerQuery(get_called_class()); } diff --git a/tests/unit/data/ar/mongodb/Customer.php b/tests/unit/data/ar/mongodb/Customer.php index 98cf41f..297422c 100644 --- a/tests/unit/data/ar/mongodb/Customer.php +++ b/tests/unit/data/ar/mongodb/Customer.php @@ -25,7 +25,11 @@ class Customer extends ActiveRecord return $this->hasMany(CustomerOrder::className(), ['customer_id' => '_id']); } - public static function createQuery() + /** + * @inheritdoc + * @return CustomerQuery + */ + public static function find() { return new CustomerQuery(get_called_class()); } diff --git a/tests/unit/data/ar/mongodb/file/CustomerFile.php b/tests/unit/data/ar/mongodb/file/CustomerFile.php index 6c97b37..5576635 100644 --- a/tests/unit/data/ar/mongodb/file/CustomerFile.php +++ b/tests/unit/data/ar/mongodb/file/CustomerFile.php @@ -4,11 +4,17 @@ namespace yiiunit\data\ar\mongodb\file; class CustomerFile extends ActiveRecord { + /** + * @inheritdoc + */ public static function collectionName() { return 'customer_fs'; } + /** + * @inheritdoc + */ public function attributes() { return array_merge( @@ -20,7 +26,11 @@ class CustomerFile extends ActiveRecord ); } - public static function createQuery() + /** + * @inheritdoc + * @return CustomerFileQuery + */ + public static function find() { return new CustomerFileQuery(get_called_class()); } diff --git a/tests/unit/data/ar/redis/Customer.php b/tests/unit/data/ar/redis/Customer.php index 60c1983..1bdf2b6 100644 --- a/tests/unit/data/ar/redis/Customer.php +++ b/tests/unit/data/ar/redis/Customer.php @@ -11,6 +11,9 @@ class Customer extends ActiveRecord public $status2; + /** + * @inheritdoc + */ public function attributes() { return ['id', 'email', 'name', 'address', 'status', 'profile_id']; @@ -24,6 +27,9 @@ class Customer extends ActiveRecord return $this->hasMany(Order::className(), ['customer_id' => 'id']); } + /** + * @inheritdoc + */ public function afterSave($insert) { ActiveRecordTest::$afterSaveInsert = $insert; @@ -31,7 +37,11 @@ class Customer extends ActiveRecord parent::afterSave($insert); } - public static function createQuery() + /** + * @inheritdoc + * @return CustomerQuery + */ + public static function find() { return new CustomerQuery(get_called_class()); } diff --git a/tests/unit/data/ar/sphinx/ArticleIndex.php b/tests/unit/data/ar/sphinx/ArticleIndex.php index 742d2d4..805c800 100644 --- a/tests/unit/data/ar/sphinx/ArticleIndex.php +++ b/tests/unit/data/ar/sphinx/ArticleIndex.php @@ -5,6 +5,9 @@ class ArticleIndex extends ActiveRecord { public $custom_column; + /** + * @inheritdoc + */ public static function indexName() { return 'yii2_test_article_index'; @@ -20,12 +23,18 @@ class ArticleIndex extends ActiveRecord return $this->hasMany(TagDb::className(), ['id' => 'tag']); } + /** + * @inheritdoc + */ public function getSnippetSource() { return $this->source->content; } - public static function createQuery() + /** + * @return ArticleIndexQuery + */ + public static function find() { return new ArticleIndexQuery(get_called_class()); } diff --git a/tests/unit/extensions/elasticsearch/ActiveRecordTest.php b/tests/unit/extensions/elasticsearch/ActiveRecordTest.php index 27447a7..bb95219 100644 --- a/tests/unit/extensions/elasticsearch/ActiveRecordTest.php +++ b/tests/unit/extensions/elasticsearch/ActiveRecordTest.php @@ -243,7 +243,7 @@ class ActiveRecordTest extends ElasticSearchTestCase public function testFindLazy() { /** @var $customer Customer */ - $customer = Customer::find(2); + $customer = Customer::findOne(2); $orders = $customer->orders; $this->assertEquals(2, count($orders)); @@ -314,7 +314,7 @@ class ActiveRecordTest extends ElasticSearchTestCase { $pkName = 'id'; - $orderItem = Order::find([$pkName => 2]); + $orderItem = Order::findOne([$pkName => 2]); $this->assertEquals(2, $orderItem->primaryKey); $this->assertEquals(2, $orderItem->oldPrimaryKey); $this->assertEquals(2, $orderItem->$pkName); @@ -330,8 +330,8 @@ class ActiveRecordTest extends ElasticSearchTestCase $this->assertEquals(13, $orderItem->oldPrimaryKey); $this->assertEquals(13, $orderItem->$pkName); - $this->assertNull(Order::find([$pkName => 2])); - $this->assertNotNull(Order::find([$pkName => 13])); + $this->assertNull(Order::findOne([$pkName => 2])); + $this->assertNotNull(Order::findOne([$pkName => 13])); } public function testFindLazyVia2() diff --git a/tests/unit/extensions/mongodb/ActiveRecordTest.php b/tests/unit/extensions/mongodb/ActiveRecordTest.php index 469d9b4..ee94e99 100644 --- a/tests/unit/extensions/mongodb/ActiveRecordTest.php +++ b/tests/unit/extensions/mongodb/ActiveRecordTest.php @@ -66,16 +66,16 @@ class ActiveRecordTest extends MongoDbTestCase // find by _id $testId = $this->testRows[0]['_id']; - $customer = Customer::find($testId); + $customer = Customer::findOne($testId); $this->assertTrue($customer instanceof Customer); $this->assertEquals($testId, $customer->_id); // find by column values - $customer = Customer::find(['name' => 'name5']); + $customer = Customer::findOne(['name' => 'name5']); $this->assertTrue($customer instanceof Customer); $this->assertEquals($this->testRows[4]['_id'], $customer->_id); $this->assertEquals('name5', $customer->name); - $customer = Customer::find(['name' => 'unexisting name']); + $customer = Customer::findOne(['name' => 'unexisting name']); $this->assertNull($customer); // find by attributes @@ -142,7 +142,7 @@ class ActiveRecordTest extends MongoDbTestCase $record->save(); // save - $record = Customer::find($record->_id); + $record = Customer::findOne($record->_id); $this->assertTrue($record instanceof Customer); $this->assertEquals(7, $record->status); $this->assertFalse($record->isNewRecord); @@ -151,14 +151,14 @@ class ActiveRecordTest extends MongoDbTestCase $record->save(); $this->assertEquals(9, $record->status); $this->assertFalse($record->isNewRecord); - $record2 = Customer::find($record->_id); + $record2 = Customer::findOne($record->_id); $this->assertEquals(9, $record2->status); // updateAll $pk = ['_id' => $record->_id]; $ret = Customer::updateAll(['status' => 55], $pk); $this->assertEquals(1, $ret); - $record = Customer::find($pk); + $record = Customer::findOne($pk); $this->assertEquals(55, $record->status); } @@ -175,9 +175,9 @@ class ActiveRecordTest extends MongoDbTestCase $record->status = 7; $record->save(); - $record = Customer::find($record->_id); + $record = Customer::findOne($record->_id); $record->delete(); - $record = Customer::find($record->_id); + $record = Customer::findOne($record->_id); $this->assertNull($record); // deleteAll @@ -198,7 +198,7 @@ class ActiveRecordTest extends MongoDbTestCase { $this->assertEquals(1, Customer::updateAllCounters(['status' => 10], ['status' => 10])); - $record = Customer::find(['status' => 10]); + $record = Customer::findOne(['status' => 10]); $this->assertNull($record); } @@ -207,14 +207,14 @@ class ActiveRecordTest extends MongoDbTestCase */ public function testUpdateCounters() { - $record = Customer::find($this->testRows[9]); + $record = Customer::findOne($this->testRows[9]); $originalCounter = $record->status; $counterIncrement = 20; $record->updateCounters(['status' => $counterIncrement]); $this->assertEquals($originalCounter + $counterIncrement, $record->status); - $refreshedRecord = Customer::find($record->_id); + $refreshedRecord = Customer::findOne($record->_id); $this->assertEquals($originalCounter + $counterIncrement, $refreshedRecord->status); } @@ -234,13 +234,13 @@ class ActiveRecordTest extends MongoDbTestCase $record->save(); // save - $record = Customer::find($record->_id); + $record = Customer::findOne($record->_id); $newAddress = [ 'city' => 'AnotherCity' ]; $record->address = $newAddress; $record->save(); - $record2 = Customer::find($record->_id); + $record2 = Customer::findOne($record->_id); $this->assertEquals($newAddress, $record2->address); } diff --git a/tests/unit/extensions/mongodb/ActiveRelationTest.php b/tests/unit/extensions/mongodb/ActiveRelationTest.php index 7025f6c..3ffabd4 100644 --- a/tests/unit/extensions/mongodb/ActiveRelationTest.php +++ b/tests/unit/extensions/mongodb/ActiveRelationTest.php @@ -63,7 +63,7 @@ class ActiveRelationTest extends MongoDbTestCase public function testFindLazy() { /** @var CustomerOrder $order */ - $order = CustomerOrder::find(['number' => 2]); + $order = CustomerOrder::findOne(['number' => 2]); $this->assertFalse($order->isRelationPopulated('customer')); $customer = $order->customer; $this->assertTrue($order->isRelationPopulated('customer')); diff --git a/tests/unit/extensions/redis/ActiveRecordTest.php b/tests/unit/extensions/redis/ActiveRecordTest.php index 837bbe3..9378992 100644 --- a/tests/unit/extensions/redis/ActiveRecordTest.php +++ b/tests/unit/extensions/redis/ActiveRecordTest.php @@ -229,7 +229,7 @@ class ActiveRecordTest extends RedisTestCase { // updateCounters $pk = ['order_id' => 2, 'item_id' => 4]; - $orderItem = OrderItem::find($pk); + $orderItem = OrderItem::findOne($pk); $this->assertEquals(2, $orderItem->order_id); $this->assertEquals(4, $orderItem->item_id); @@ -237,8 +237,8 @@ class ActiveRecordTest extends RedisTestCase $orderItem->item_id = 10; $orderItem->save(); - $this->assertNull(OrderItem::find($pk)); - $this->assertNotNull(OrderItem::find(['order_id' => 2, 'item_id' => 10])); + $this->assertNull(OrderItem::findOne($pk)); + $this->assertNotNull(OrderItem::findOne(['order_id' => 2, 'item_id' => 10])); } public function testFilterWhere() diff --git a/tests/unit/extensions/sphinx/ActiveRecordTest.php b/tests/unit/extensions/sphinx/ActiveRecordTest.php index cbf1519..cdf8a52 100644 --- a/tests/unit/extensions/sphinx/ActiveRecordTest.php +++ b/tests/unit/extensions/sphinx/ActiveRecordTest.php @@ -41,16 +41,16 @@ class ActiveRecordTest extends SphinxTestCase $this->assertTrue($articles[1] instanceof ArticleIndex); // find fulltext - $article = ArticleIndex::find(2); + $article = ArticleIndex::findOne(2); $this->assertTrue($article instanceof ArticleIndex); $this->assertEquals(2, $article->id); // find by column values - $article = ArticleIndex::find(['id' => 2, 'author_id' => 2]); + $article = ArticleIndex::findOne(['id' => 2, 'author_id' => 2]); $this->assertTrue($article instanceof ArticleIndex); $this->assertEquals(2, $article->id); $this->assertEquals(2, $article->author_id); - $article = ArticleIndex::find(['id' => 2, 'author_id' => 1]); + $article = ArticleIndex::findOne(['id' => 2, 'author_id' => 1]); $this->assertNull($article); // find by attributes @@ -148,7 +148,7 @@ class ActiveRecordTest extends SphinxTestCase $record->save(); // save - $record = RuntimeIndex::find(2); + $record = RuntimeIndex::findOne(2); $this->assertTrue($record instanceof RuntimeIndex); $this->assertEquals(7, $record->type_id); $this->assertFalse($record->isNewRecord); @@ -157,14 +157,14 @@ class ActiveRecordTest extends SphinxTestCase $record->save(); $this->assertEquals(9, $record->type_id); $this->assertFalse($record->isNewRecord); - $record2 = RuntimeIndex::find(['id' => 2]); + $record2 = RuntimeIndex::findOne(['id' => 2]); $this->assertEquals(9, $record2->type_id); // replace $query = 'replace'; $rows = RuntimeIndex::find()->match($query)->all(); $this->assertEmpty($rows); - $record = RuntimeIndex::find(2); + $record = RuntimeIndex::findOne(2); $record->content = 'Test content with ' . $query; $record->save(); $rows = RuntimeIndex::find()->match($query); @@ -174,7 +174,7 @@ class ActiveRecordTest extends SphinxTestCase $pk = ['id' => 2]; $ret = RuntimeIndex::updateAll(['type_id' => 55], $pk); $this->assertEquals(1, $ret); - $record = RuntimeIndex::find($pk); + $record = RuntimeIndex::findOne($pk); $this->assertEquals(55, $record->type_id); } @@ -192,9 +192,9 @@ class ActiveRecordTest extends SphinxTestCase $record->category = [1, 2]; $record->save(); - $record = RuntimeIndex::find(2); + $record = RuntimeIndex::findOne(2); $record->delete(); - $record = RuntimeIndex::find(2); + $record = RuntimeIndex::findOne(2); $this->assertNull($record); // deleteAll diff --git a/tests/unit/extensions/sphinx/ActiveRelationTest.php b/tests/unit/extensions/sphinx/ActiveRelationTest.php index 4d783d6..35f389e 100644 --- a/tests/unit/extensions/sphinx/ActiveRelationTest.php +++ b/tests/unit/extensions/sphinx/ActiveRelationTest.php @@ -24,7 +24,7 @@ class ActiveRelationTest extends SphinxTestCase public function testFindLazy() { /** @var ArticleDb $article */ - $article = ArticleDb::find(['id' => 2]); + $article = ArticleDb::findOne(['id' => 2]); $this->assertFalse($article->isRelationPopulated('index')); $index = $article->index; $this->assertTrue($article->isRelationPopulated('index')); diff --git a/tests/unit/extensions/sphinx/ExternalActiveRelationTest.php b/tests/unit/extensions/sphinx/ExternalActiveRelationTest.php index ae38a01..5a74254 100644 --- a/tests/unit/extensions/sphinx/ExternalActiveRelationTest.php +++ b/tests/unit/extensions/sphinx/ExternalActiveRelationTest.php @@ -24,7 +24,7 @@ class ExternalActiveRelationTest extends SphinxTestCase public function testFindLazy() { /** @var ArticleIndex $article */ - $article = ArticleIndex::find(['id' => 2]); + $article = ArticleIndex::findOne(['id' => 2]); // has one : $this->assertFalse($article->isRelationPopulated('source')); diff --git a/tests/unit/framework/ar/ActiveRecordTestTrait.php b/tests/unit/framework/ar/ActiveRecordTestTrait.php index 69bb864..e52bb70 100644 --- a/tests/unit/framework/ar/ActiveRecordTestTrait.php +++ b/tests/unit/framework/ar/ActiveRecordTestTrait.php @@ -90,25 +90,25 @@ trait ActiveRecordTestTrait $this->assertArrayHasKey('status', $customers[2]); // find by a single primary key - $customer = $customerClass::find(2); + $customer = $customerClass::findOne(2); $this->assertTrue($customer instanceof $customerClass); $this->assertEquals('user2', $customer->name); - $customer = $customerClass::find(5); + $customer = $customerClass::findOne(5); $this->assertNull($customer); - $customer = $customerClass::find(['id' => [5, 6, 1]]); + $customer = $customerClass::findOne(['id' => [5, 6, 1]]); $this->assertEquals(1, count($customer)); $customer = $customerClass::find()->where(['id' => [5, 6, 1]])->one(); $this->assertNotNull($customer); // find by column values - $customer = $customerClass::find(['id' => 2, 'name' => 'user2']); + $customer = $customerClass::findOne(['id' => 2, 'name' => 'user2']); $this->assertTrue($customer instanceof $customerClass); $this->assertEquals('user2', $customer->name); - $customer = $customerClass::find(['id' => 2, 'name' => 'user1']); + $customer = $customerClass::findOne(['id' => 2, 'name' => 'user1']); $this->assertNull($customer); - $customer = $customerClass::find(['id' => 5]); + $customer = $customerClass::findOne(['id' => 5]); $this->assertNull($customer); - $customer = $customerClass::find(['name' => 'user5']); + $customer = $customerClass::findOne(['name' => 'user5']); $this->assertNull($customer); // find by attributes @@ -242,7 +242,7 @@ trait ActiveRecordTestTrait $customer = new $customerClass(); $this->assertFalse($customer->refresh()); - $customer = $customerClass::find(1); + $customer = $customerClass::findOne(1); $customer->name = 'to be refreshed'; $this->assertTrue($customer->refresh()); $this->assertEquals('user1', $customer->name); @@ -264,15 +264,15 @@ trait ActiveRecordTestTrait $customerB = new $itemClass(); $this->assertFalse($customerA->equals($customerB)); - $customerA = $customerClass::find(1); - $customerB = $customerClass::find(2); + $customerA = $customerClass::findOne(1); + $customerB = $customerClass::findOne(2); $this->assertFalse($customerA->equals($customerB)); - $customerB = $customerClass::find(1); + $customerB = $customerClass::findOne(1); $this->assertTrue($customerA->equals($customerB)); - $customerA = $customerClass::find(1); - $customerB = $itemClass::find(1); + $customerA = $customerClass::findOne(1); + $customerB = $itemClass::findOne(1); $this->assertFalse($customerA->equals($customerB)); } @@ -366,7 +366,7 @@ trait ActiveRecordTestTrait $customerClass = $this->getCustomerClass(); /** @var TestCase|ActiveRecordTestTrait $this */ - $customer = $customerClass::find(2); + $customer = $customerClass::findOne(2); $customer->name = null; $customer->save(false); $this->afterSave(); @@ -398,7 +398,7 @@ trait ActiveRecordTestTrait $customerClass = $this->getCustomerClass(); /** @var TestCase|ActiveRecordTestTrait $this */ - $customer = $customerClass::find(2); + $customer = $customerClass::findOne(2); $this->assertFalse($customer->isRelationPopulated('orders')); $orders = $customer->orders; $this->assertTrue($customer->isRelationPopulated('orders')); @@ -410,7 +410,7 @@ trait ActiveRecordTestTrait $this->assertFalse($customer->isRelationPopulated('orders')); /** @var Customer $customer */ - $customer = $customerClass::find(2); + $customer = $customerClass::findOne(2); $this->assertFalse($customer->isRelationPopulated('orders')); $orders = $customer->getOrders()->where(['id' => 3])->all(); $this->assertFalse($customer->isRelationPopulated('orders')); @@ -464,7 +464,7 @@ trait ActiveRecordTestTrait /** @var TestCase|ActiveRecordTestTrait $this */ /** @var Order $order */ - $order = $orderClass::find(1); + $order = $orderClass::findOne(1); $this->assertEquals(1, $order->id); $this->assertEquals(2, count($order->items)); $this->assertEquals(1, $order->items[0]->id); @@ -478,7 +478,7 @@ trait ActiveRecordTestTrait /** @var TestCase|ActiveRecordTestTrait $this */ /** @var Order $order */ - $order = $orderClass::find(1); + $order = $orderClass::findOne(1); $order->id = 100; $this->assertEquals([], $order->items); } @@ -630,7 +630,7 @@ trait ActiveRecordTestTrait $orderItemClass = $this->getOrderItemClass(); $itemClass = $this->getItemClass(); /** @var TestCase|ActiveRecordTestTrait $this */ - $customer = $customerClass::find(2); + $customer = $customerClass::findOne(2); $this->assertEquals(2, count($customer->orders)); // has many @@ -648,7 +648,7 @@ trait ActiveRecordTestTrait $order = new $orderClass; $order->total = 100; $this->assertTrue($order->isNewRecord); - $customer = $customerClass::find(1); + $customer = $customerClass::findOne(1); $this->assertNull($order->customer); $order->link('customer', $customer); $this->assertFalse($order->isNewRecord); @@ -656,17 +656,17 @@ trait ActiveRecordTestTrait $this->assertEquals(1, $order->customer->primaryKey); // via model - $order = $orderClass::find(1); + $order = $orderClass::findOne(1); $this->assertEquals(2, count($order->items)); $this->assertEquals(2, count($order->orderItems)); - $orderItem = $orderItemClass::find(['order_id' => 1, 'item_id' => 3]); + $orderItem = $orderItemClass::findOne(['order_id' => 1, 'item_id' => 3]); $this->assertNull($orderItem); - $item = $itemClass::find(3); + $item = $itemClass::findOne(3); $order->link('items', $item, ['quantity' => 10, 'subtotal' => 100]); $this->afterSave(); $this->assertEquals(3, count($order->items)); $this->assertEquals(3, count($order->orderItems)); - $orderItem = $orderItemClass::find(['order_id' => 1, 'item_id' => 3]); + $orderItem = $orderItemClass::findOne(['order_id' => 1, 'item_id' => 3]); $this->assertTrue($orderItem instanceof $orderItemClass); $this->assertEquals(10, $orderItem->quantity); $this->assertEquals(100, $orderItem->subtotal); @@ -681,15 +681,15 @@ trait ActiveRecordTestTrait /** @var TestCase|ActiveRecordTestTrait $this */ // has many - $customer = $customerClass::find(2); + $customer = $customerClass::findOne(2); $this->assertEquals(2, count($customer->orders)); $customer->unlink('orders', $customer->orders[1], true); $this->afterSave(); $this->assertEquals(1, count($customer->orders)); - $this->assertNull($orderClass::find(3)); + $this->assertNull($orderClass::findOne(3)); // via model - $order = $orderClass::find(2); + $order = $orderClass::findOne(2); $this->assertEquals(3, count($order->items)); $this->assertEquals(3, count($order->orderItems)); $order->unlink('items', $order->items[2], true); @@ -731,7 +731,7 @@ trait ActiveRecordTestTrait $customerClass = $this->getCustomerClass(); /** @var TestCase|ActiveRecordTestTrait $this */ // save - $customer = $customerClass::find(2); + $customer = $customerClass::findOne(2); $this->assertTrue($customer instanceof $customerClass); $this->assertEquals('user2', $customer->name); $this->assertFalse($customer->isNewRecord); @@ -745,16 +745,16 @@ trait ActiveRecordTestTrait $this->assertFalse($customer->isNewRecord); $this->assertFalse(static::$afterSaveNewRecord); $this->assertFalse(static::$afterSaveInsert); - $customer2 = $customerClass::find(2); + $customer2 = $customerClass::findOne(2); $this->assertEquals('user2x', $customer2->name); // updateAll - $customer = $customerClass::find(3); + $customer = $customerClass::findOne(3); $this->assertEquals('user3', $customer->name); $ret = $customerClass::updateAll(['name' => 'temp'], ['id' => 3]); $this->afterSave(); $this->assertEquals(1, $ret); - $customer = $customerClass::find(3); + $customer = $customerClass::findOne(3); $this->assertEquals('temp', $customer->name); $ret = $customerClass::updateAll(['name' => 'tempX']); @@ -772,7 +772,7 @@ trait ActiveRecordTestTrait $customerClass = $this->getCustomerClass(); /** @var TestCase|ActiveRecordTestTrait $this */ // save - $customer = $customerClass::find(2); + $customer = $customerClass::findOne(2); $this->assertTrue($customer instanceof $customerClass); $this->assertEquals('user2', $customer->name); $this->assertFalse($customer->isNewRecord); @@ -785,10 +785,10 @@ trait ActiveRecordTestTrait $this->assertFalse($customer->isNewRecord); $this->assertFalse(static::$afterSaveNewRecord); $this->assertFalse(static::$afterSaveInsert); - $customer2 = $customerClass::find(2); + $customer2 = $customerClass::findOne(2); $this->assertEquals('user2x', $customer2->name); - $customer = $customerClass::find(1); + $customer = $customerClass::findOne(1); $this->assertEquals('user1', $customer->name); $this->assertEquals(1, $customer->status); $customer->name = 'user1x'; @@ -796,7 +796,7 @@ trait ActiveRecordTestTrait $customer->updateAttributes(['name']); $this->assertEquals('user1x', $customer->name); $this->assertEquals(2, $customer->status); - $customer = $customerClass::find(1); + $customer = $customerClass::findOne(1); $this->assertEquals('user1x', $customer->name); $this->assertEquals(1, $customer->status); } @@ -808,18 +808,18 @@ trait ActiveRecordTestTrait /** @var TestCase|ActiveRecordTestTrait $this */ // updateCounters $pk = ['order_id' => 2, 'item_id' => 4]; - $orderItem = $orderItemClass::find($pk); + $orderItem = $orderItemClass::findOne($pk); $this->assertEquals(1, $orderItem->quantity); $ret = $orderItem->updateCounters(['quantity' => -1]); $this->afterSave(); $this->assertEquals(1, $ret); $this->assertEquals(0, $orderItem->quantity); - $orderItem = $orderItemClass::find($pk); + $orderItem = $orderItemClass::findOne($pk); $this->assertEquals(0, $orderItem->quantity); // updateAllCounters $pk = ['order_id' => 1, 'item_id' => 2]; - $orderItem = $orderItemClass::find($pk); + $orderItem = $orderItemClass::findOne($pk); $this->assertEquals(2, $orderItem->quantity); $ret = $orderItemClass::updateAllCounters([ 'quantity' => 3, @@ -827,7 +827,7 @@ trait ActiveRecordTestTrait ], $pk); $this->afterSave(); $this->assertEquals(1, $ret); - $orderItem = $orderItemClass::find($pk); + $orderItem = $orderItemClass::findOne($pk); $this->assertEquals(5, $orderItem->quantity); $this->assertEquals(30, $orderItem->subtotal); } @@ -838,12 +838,12 @@ trait ActiveRecordTestTrait $customerClass = $this->getCustomerClass(); /** @var TestCase|ActiveRecordTestTrait $this */ // delete - $customer = $customerClass::find(2); + $customer = $customerClass::findOne(2); $this->assertTrue($customer instanceof $customerClass); $this->assertEquals('user2', $customer->name); $customer->delete(); $this->afterSave(); - $customer = $customerClass::find(2); + $customer = $customerClass::findOne(2); $this->assertNull($customer); // deleteAll @@ -906,7 +906,7 @@ trait ActiveRecordTestTrait $afterFindCalls[] = [get_class($ar), $ar->getIsNewRecord(), $ar->getPrimaryKey(), $ar->isRelationPopulated('orders')]; }); - $customer = $customerClass::find(1); + $customer = $customerClass::findOne(1); $this->assertNotNull($customer); $this->assertEquals([[$customerClass, false, 1, false]], $afterFindCalls); $afterFindCalls = []; diff --git a/tests/unit/framework/data/ActiveDataProviderTest.php b/tests/unit/framework/data/ActiveDataProviderTest.php index 607c71b..e13bc3a 100644 --- a/tests/unit/framework/data/ActiveDataProviderTest.php +++ b/tests/unit/framework/data/ActiveDataProviderTest.php @@ -55,7 +55,7 @@ class ActiveDataProviderTest extends DatabaseTestCase public function testActiveRelation() { /** @var Customer $customer */ - $customer = Customer::find(2); + $customer = Customer::findOne(2); $provider = new ActiveDataProvider([ 'query' => $customer->getOrders(), ]); @@ -78,7 +78,7 @@ class ActiveDataProviderTest extends DatabaseTestCase public function testActiveRelationVia() { /** @var Order $order */ - $order = Order::find(2); + $order = Order::findOne(2); $provider = new ActiveDataProvider([ 'query' => $order->getItems(), ]); @@ -102,7 +102,7 @@ class ActiveDataProviderTest extends DatabaseTestCase public function testActiveRelationViaTable() { /** @var Order $order */ - $order = Order::find(1); + $order = Order::findOne(1); $provider = new ActiveDataProvider([ 'query' => $order->getBooks(), ]); diff --git a/tests/unit/framework/db/ActiveRecordTest.php b/tests/unit/framework/db/ActiveRecordTest.php index 0c08978..97c9f22 100644 --- a/tests/unit/framework/db/ActiveRecordTest.php +++ b/tests/unit/framework/db/ActiveRecordTest.php @@ -101,13 +101,13 @@ class ActiveRecordTest extends DatabaseTestCase public function testFindLazyViaTable() { /** @var Order $order */ - $order = Order::find(1); + $order = Order::findOne(1); $this->assertEquals(1, $order->id); $this->assertEquals(2, count($order->books)); $this->assertEquals(1, $order->items[0]->id); $this->assertEquals(2, $order->items[1]->id); - $order = Order::find(2); + $order = Order::findOne(2); $this->assertEquals(2, $order->id); $this->assertEquals(0, count($order->books)); } @@ -148,7 +148,7 @@ class ActiveRecordTest extends DatabaseTestCase public function testDeeplyNestedTableRelation() { /** @var Customer $customer */ - $customer = Customer::find(1); + $customer = Customer::findOne(1); $this->assertNotNull($customer); $items = $customer->orderItems; @@ -356,11 +356,11 @@ class ActiveRecordTest extends DatabaseTestCase $this->assertEquals(1, count($orders[2]->books2)); // lazy loading with ON condition - $order = Order::find(1); + $order = Order::findOne(1); $this->assertEquals(2, count($order->books2)); - $order = Order::find(2); + $order = Order::findOne(2); $this->assertEquals(0, count($order->books2)); - $order = Order::find(3); + $order = Order::findOne(3); $this->assertEquals(1, count($order->books2)); // eager loading with ON condition @@ -384,7 +384,7 @@ class ActiveRecordTest extends DatabaseTestCase $this->assertEquals(3, count($orders)); // https://github.com/yiisoft/yii2/issues/2880 - $query = Order::find(1); + $query = Order::findOne(1); $customer = $query->getCustomer()->joinWith([ 'orders' => function ($q) { $q->orderBy([]); } ])->one(); @@ -451,13 +451,13 @@ class ActiveRecordTest extends DatabaseTestCase $this->assertTrue($customers[0]->orders2[0]->customer2 === $customers[0]); $this->assertTrue(empty($customers[1]->orders2)); // lazy loading - $customer = Customer::find(2); + $customer = Customer::findOne(2); $orders = $customer->orders2; $this->assertTrue(count($orders) === 2); $this->assertTrue($customer->orders2[0]->customer2 === $customer); $this->assertTrue($customer->orders2[1]->customer2 === $customer); // ad-hoc lazy loading - $customer = Customer::find(2); + $customer = Customer::findOne(2); $orders = $customer->getOrders2()->all(); $this->assertTrue(count($orders) === 2); $this->assertTrue($customer->orders2[0]->customer2 === $customer); diff --git a/tests/unit/framework/validators/ExistValidatorTest.php b/tests/unit/framework/validators/ExistValidatorTest.php index 76912e1..987f4fe 100644 --- a/tests/unit/framework/validators/ExistValidatorTest.php +++ b/tests/unit/framework/validators/ExistValidatorTest.php @@ -101,7 +101,7 @@ class ExistValidatorTest extends DatabaseTestCase 'targetAttribute' => ['order_id', 'item_id'], ]); // validate old record - $m = OrderItem::find(['order_id' => 1, 'item_id' => 2]); + $m = OrderItem::findOne(['order_id' => 1, 'item_id' => 2]); $val->validateAttribute($m, 'order_id'); $this->assertFalse($m->hasErrors('order_id')); @@ -118,10 +118,10 @@ class ExistValidatorTest extends DatabaseTestCase 'targetAttribute' => ['id' => 'order_id'], ]); // validate old record - $m = Order::find(1); + $m = Order::findOne(1); $val->validateAttribute($m, 'id'); $this->assertFalse($m->hasErrors('id')); - $m = Order::find(1); + $m = Order::findOne(1); $m->id = 10; $val->validateAttribute($m, 'id'); $this->assertTrue($m->hasErrors('id'));