Commit 75154d35 by Alexander Makarov

Refactored AR find

parent c7a22272
......@@ -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,
]);
......
......@@ -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,
]);
......
......@@ -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();
......
......@@ -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]);
}
```
......
......@@ -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]);
}
/**
......
......@@ -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;
}
......
......@@ -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();
}
......
......@@ -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',
......
......@@ -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]);
}
}
```
......
......@@ -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);
```
......
......@@ -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
/**
......
......@@ -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.');
......
......@@ -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());
}
......
......@@ -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());
}
......
......@@ -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();
......
......@@ -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());
}
......
......@@ -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();
......
......@@ -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.
......
......@@ -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.
......
......@@ -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,
* ]);
......
......@@ -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)) {
......
......@@ -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);
}
/**
......
......@@ -18,7 +18,7 @@ namespace yii\web;
* {
* public static function findIdentity($id)
* {
* return static::find($id);
* return static::findOne($id);
* }
*
* public function getId()
......
......@@ -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());
}
......
......@@ -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());
}
......
......@@ -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());
}
......
......@@ -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());
}
......
......@@ -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());
}
......
......@@ -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());
}
......
......@@ -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()
......
......@@ -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);
}
......
......@@ -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'));
......
......@@ -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()
......
......@@ -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
......
......@@ -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'));
......
......@@ -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'));
......
......@@ -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(),
]);
......
......@@ -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);
......
......@@ -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'));
......
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