Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
75154d35
Commit
75154d35
authored
Apr 07, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored AR find
parent
c7a22272
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
40 changed files
with
215 additions
and
328 deletions
+215
-328
User.php
apps/advanced/common/models/User.php
+3
-3
PasswordResetRequestForm.php
apps/advanced/frontend/models/PasswordResetRequestForm.php
+1
-1
PasswordResetRequestFormTest.php
...ontend/tests/unit/models/PasswordResetRequestFormTest.php
+1
-1
active-record.md
docs/guide/active-record.md
+27
-51
authentication.md
docs/guide/authentication.md
+2
-2
authorization.md
docs/guide/authorization.md
+2
-2
controller.md
docs/guide/controller.md
+2
-2
model.md
docs/guide/model.md
+5
-5
rest.md
docs/guide/rest.md
+1
-1
upgrade-from-v1.md
docs/guide/upgrade-from-v1.md
+1
-1
ActiveRecord.php
extensions/elasticsearch/ActiveRecord.php
+12
-37
controller.php
extensions/gii/generators/crud/default/controller.php
+1
-1
ActiveRecord.php
extensions/mongodb/ActiveRecord.php
+2
-21
ActiveRecord.php
extensions/mongodb/file/ActiveRecord.php
+2
-21
ActiveRecord.php
extensions/redis/ActiveRecord.php
+3
-22
ActiveRecord.php
extensions/sphinx/ActiveRecord.php
+3
-22
ActiveRecord.php
framework/db/ActiveRecord.php
+4
-23
ActiveRecordInterface.php
framework/db/ActiveRecordInterface.php
+32
-35
BaseActiveRecord.php
framework/db/BaseActiveRecord.php
+12
-18
HttpBasicAuth.php
framework/filters/auth/HttpBasicAuth.php
+1
-1
Action.php
framework/rest/Action.php
+2
-2
BaseActiveFixture.php
framework/test/BaseActiveFixture.php
+1
-1
IdentityInterface.php
framework/web/IdentityInterface.php
+1
-1
Customer.php
tests/unit/data/ar/Customer.php
+5
-2
Customer.php
tests/unit/data/ar/elasticsearch/Customer.php
+5
-1
Customer.php
tests/unit/data/ar/mongodb/Customer.php
+5
-1
CustomerFile.php
tests/unit/data/ar/mongodb/file/CustomerFile.php
+11
-1
Customer.php
tests/unit/data/ar/redis/Customer.php
+11
-1
ArticleIndex.php
tests/unit/data/ar/sphinx/ArticleIndex.php
+10
-1
ActiveRecordTest.php
tests/unit/extensions/elasticsearch/ActiveRecordTest.php
+4
-4
ActiveRecordTest.php
tests/unit/extensions/mongodb/ActiveRecordTest.php
+13
-13
ActiveRelationTest.php
tests/unit/extensions/mongodb/ActiveRelationTest.php
+1
-1
ActiveRecordTest.php
tests/unit/extensions/redis/ActiveRecordTest.php
+3
-3
ActiveRecordTest.php
tests/unit/extensions/sphinx/ActiveRecordTest.php
+9
-9
ActiveRelationTest.php
tests/unit/extensions/sphinx/ActiveRelationTest.php
+1
-1
ExternalActiveRelationTest.php
tests/unit/extensions/sphinx/ExternalActiveRelationTest.php
+1
-1
ActiveRecordTestTrait.php
tests/unit/framework/ar/ActiveRecordTestTrait.php
+0
-0
ActiveDataProviderTest.php
tests/unit/framework/data/ActiveDataProviderTest.php
+3
-3
ActiveRecordTest.php
tests/unit/framework/db/ActiveRecordTest.php
+9
-9
ExistValidatorTest.php
tests/unit/framework/validators/ExistValidatorTest.php
+3
-3
No files found.
apps/advanced/common/models/User.php
View file @
75154d35
...
...
@@ -69,7 +69,7 @@ class User extends ActiveRecord implements IdentityInterface
*/
public
static
function
findIdentity
(
$id
)
{
return
static
::
find
(
$id
);
return
static
::
find
One
(
$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
::
find
One
([
'username'
=>
$username
,
'status'
=>
self
::
STATUS_ACTIVE
]);
}
/**
...
...
@@ -107,7 +107,7 @@ class User extends ActiveRecord implements IdentityInterface
return
null
;
}
return
static
::
find
([
return
static
::
find
One
([
'password_reset_token'
=>
$token
,
'status'
=>
self
::
STATUS_ACTIVE
,
]);
...
...
apps/advanced/frontend/models/PasswordResetRequestForm.php
View file @
75154d35
...
...
@@ -36,7 +36,7 @@ class PasswordResetRequestForm extends Model
public
function
sendEmail
()
{
/** @var User $user */
$user
=
User
::
find
([
$user
=
User
::
find
One
([
'status'
=>
User
::
STATUS_ACTIVE
,
'email'
=>
$this
->
email
,
]);
...
...
apps/advanced/frontend/tests/unit/models/PasswordResetRequestFormTest.php
View file @
75154d35
...
...
@@ -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
::
find
One
([
'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
();
...
...
docs/guide/active-record.md
View file @
75154d35
...
...
@@ -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 Recor
d
instance directly without the need
to call
`one()`
.
The
re 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 nee
d
to call
`one()`
.
```
php
// to return a single customer whose ID is 1:
$customer
=
Customer
::
find
(
1
);
$customer
=
Customer
::
find
One
(
1
);
// to return an *active* customer whose ID is 1:
$customer
=
Customer
::
find
([
$customer
=
Customer
::
find
One
([
'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
::
find
One
(
$id
);
$customer
->
email
=
'james@example.com'
;
$customer
->
save
();
// equivalent to $customer->update();
// to delete an existing customer record
$customer
=
Customer
::
find
(
$id
);
$customer
=
Customer
::
find
One
(
$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
::
find
One
(
$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
::
find
One
(
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
::
find
One
(
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
::
find
One
(
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
::
find
One
(
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
::
find
One
(
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
::
find
One
(
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
::
find
One
(
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
]);
}
```
...
...
docs/guide/authentication.md
View file @
75154d35
...
...
@@ -21,7 +21,7 @@ class User extends ActiveRecord implements IdentityInterface
*/
public
static
function
findIdentity
(
$id
)
{
return
static
::
find
(
$id
);
return
static
::
find
One
(
$id
);
}
/**
...
...
@@ -32,7 +32,7 @@ class User extends ActiveRecord implements IdentityInterface
*/
public
static
function
findIdentityByAccessToken
(
$token
)
{
return
static
::
find
([
'access_token'
=>
$token
]);
return
static
::
find
One
([
'access_token'
=>
$token
]);
}
/**
...
...
docs/guide/authorization.md
View file @
75154d35
...
...
@@ -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
::
find
One
(
$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
::
find
One
([
'id'
=>
$id
,
'author_id'
=>
\Yii
::
$app
->
user
->
id
]);
if
(
!
$article
)
{
throw
new
NotFoundHttpException
;
}
...
...
docs/guide/controller.md
View file @
75154d35
...
...
@@ -91,7 +91,7 @@ class BlogController extends Controller
{
public
function
actionView
(
$id
,
$version
=
null
)
{
$post
=
Post
::
find
(
$id
);
$post
=
Post
::
find
One
(
$id
);
$text
=
$post
->
text
;
if
(
$version
)
{
...
...
@@ -125,7 +125,7 @@ class BlogController extends Controller
{
public
function
actionUpdate
(
$id
)
{
$post
=
Post
::
find
(
$id
);
$post
=
Post
::
find
One
(
$id
);
if
(
!
$post
)
{
throw
new
NotFoundHttpException
();
}
...
...
docs/guide/model.md
View file @
75154d35
...
...
@@ -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
::
find
One
(
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
::
find
One
(
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
::
find
One
(
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
::
find
One
(
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
::
find
One
(
42
);
$data
=
[
'username'
=>
'samdark'
,
'first_name'
=>
'Alexander'
,
...
...
docs/guide/rest.md
View file @
75154d35
...
...
@@ -663,7 +663,7 @@ class User extends ActiveRecord implements IdentityInterface
{
public
static
function
findIdentityByAccessToken
(
$token
)
{
return
static
::
find
([
'access_token'
=>
$token
]);
return
static
::
find
One
([
'access_token'
=>
$token
]);
}
}
```
...
...
docs/guide/upgrade-from-v1.md
View file @
75154d35
...
...
@@ -454,7 +454,7 @@ $customers = Customer::find()
->
orderBy
(
'id'
)
->
all
();
// return the customer whose PK is 1
$customer
=
Customer
::
find
(
1
);
$customer
=
Customer
::
find
One
(
1
);
```
...
...
extensions/elasticsearch/ActiveRecord.php
View file @
75154d35
...
...
@@ -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
/**
...
...
extensions/gii/generators/crud/default/controller.php
View file @
75154d35
...
...
@@ -150,7 +150,7 @@ if (count($pks) === 1) {
$condition
=
'['
.
implode
(
', '
,
$condition
)
.
']'
;
}
?>
if (($model =
<?=
$modelClass
?>
::find(
<?=
$condition
?>
)) !== null) {
if (($model =
<?=
$modelClass
?>
::find
One
(
<?=
$condition
?>
)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
...
...
extensions/mongodb/ActiveRecord.php
View file @
75154d35
...
...
@@ -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
());
}
...
...
extensions/mongodb/file/ActiveRecord.php
View file @
75154d35
...
...
@@ -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
());
}
...
...
extensions/redis/ActiveRecord.php
View file @
75154d35
...
...
@@ -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
();
...
...
extensions/sphinx/ActiveRecord.php
View file @
75154d35
...
...
@@ -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
());
}
...
...
framework/db/ActiveRecord.php
View file @
75154d35
...
...
@@ -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::find
One
($id);
* $customer->name = $name;
* $customer->email = $email;
* $customer->update();
...
...
framework/db/ActiveRecordInterface.php
View file @
75154d35
...
...
@@ -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(), find
All
()).
* after executing a find method (e.g. find(), find
One
()).
* 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::find
One
(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::find
One
(['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.
...
...
framework/db/BaseActiveRecord.php
View file @
75154d35
...
...
@@ -94,22 +94,16 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
/**
* @inheritdoc
*/
public
static
function
find
(
)
public
static
function
find
One
(
$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::find
One
($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::find
One
($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::find
One
($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
->
find
One
(
$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.
...
...
framework/filters/auth/HttpBasicAuth.php
View file @
75154d35
...
...
@@ -44,7 +44,7 @@ class HttpBasicAuth extends AuthMethod
*
* ```php
* function ($username, $password) {
* return \app\models\User::find([
* return \app\models\User::find
One
([
* 'username' => $username,
* 'password' => $password,
* ]);
...
...
framework/rest/Action.php
View file @
75154d35
...
...
@@ -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
::
find
One
(
array_combine
(
$keys
,
$values
));
}
}
elseif
(
$id
!==
null
)
{
$model
=
$modelClass
::
find
(
$id
);
$model
=
$modelClass
::
find
One
(
$id
);
}
if
(
isset
(
$model
))
{
...
...
framework/test/BaseActiveFixture.php
View file @
75154d35
...
...
@@ -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
::
find
One
(
$keys
);
}
/**
...
...
framework/web/IdentityInterface.php
View file @
75154d35
...
...
@@ -18,7 +18,7 @@ namespace yii\web;
* {
* public static function findIdentity($id)
* {
* return static::find($id);
* return static::find
One
($id);
* }
*
* public function getId()
...
...
tests/unit/data/ar/Customer.php
View file @
75154d35
...
...
@@ -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
());
}
...
...
tests/unit/data/ar/elasticsearch/Customer.php
View file @
75154d35
...
...
@@ -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
());
}
...
...
tests/unit/data/ar/mongodb/Customer.php
View file @
75154d35
...
...
@@ -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
());
}
...
...
tests/unit/data/ar/mongodb/file/CustomerFile.php
View file @
75154d35
...
...
@@ -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
());
}
...
...
tests/unit/data/ar/redis/Customer.php
View file @
75154d35
...
...
@@ -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
());
}
...
...
tests/unit/data/ar/sphinx/ArticleIndex.php
View file @
75154d35
...
...
@@ -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
());
}
...
...
tests/unit/extensions/elasticsearch/ActiveRecordTest.php
View file @
75154d35
...
...
@@ -243,7 +243,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
public
function
testFindLazy
()
{
/** @var $customer Customer */
$customer
=
Customer
::
find
(
2
);
$customer
=
Customer
::
find
One
(
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
::
find
One
([
$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
::
find
One
([
$pkName
=>
2
]));
$this
->
assertNotNull
(
Order
::
find
One
([
$pkName
=>
13
]));
}
public
function
testFindLazyVia2
()
...
...
tests/unit/extensions/mongodb/ActiveRecordTest.php
View file @
75154d35
...
...
@@ -66,16 +66,16 @@ class ActiveRecordTest extends MongoDbTestCase
// find by _id
$testId
=
$this
->
testRows
[
0
][
'_id'
];
$customer
=
Customer
::
find
(
$testId
);
$customer
=
Customer
::
find
One
(
$testId
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
$testId
,
$customer
->
_id
);
// find by column values
$customer
=
Customer
::
find
([
'name'
=>
'name5'
]);
$customer
=
Customer
::
find
One
([
'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
::
find
One
([
'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
::
find
One
(
$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
::
find
One
(
$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
::
find
One
(
$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
::
find
One
(
$record
->
_id
);
$record
->
delete
();
$record
=
Customer
::
find
(
$record
->
_id
);
$record
=
Customer
::
find
One
(
$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
::
find
One
([
'status'
=>
10
]);
$this
->
assertNull
(
$record
);
}
...
...
@@ -207,14 +207,14 @@ class ActiveRecordTest extends MongoDbTestCase
*/
public
function
testUpdateCounters
()
{
$record
=
Customer
::
find
(
$this
->
testRows
[
9
]);
$record
=
Customer
::
find
One
(
$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
::
find
One
(
$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
::
find
One
(
$record
->
_id
);
$newAddress
=
[
'city'
=>
'AnotherCity'
];
$record
->
address
=
$newAddress
;
$record
->
save
();
$record2
=
Customer
::
find
(
$record
->
_id
);
$record2
=
Customer
::
find
One
(
$record
->
_id
);
$this
->
assertEquals
(
$newAddress
,
$record2
->
address
);
}
...
...
tests/unit/extensions/mongodb/ActiveRelationTest.php
View file @
75154d35
...
...
@@ -63,7 +63,7 @@ class ActiveRelationTest extends MongoDbTestCase
public
function
testFindLazy
()
{
/** @var CustomerOrder $order */
$order
=
CustomerOrder
::
find
([
'number'
=>
2
]);
$order
=
CustomerOrder
::
find
One
([
'number'
=>
2
]);
$this
->
assertFalse
(
$order
->
isRelationPopulated
(
'customer'
));
$customer
=
$order
->
customer
;
$this
->
assertTrue
(
$order
->
isRelationPopulated
(
'customer'
));
...
...
tests/unit/extensions/redis/ActiveRecordTest.php
View file @
75154d35
...
...
@@ -229,7 +229,7 @@ class ActiveRecordTest extends RedisTestCase
{
// updateCounters
$pk
=
[
'order_id'
=>
2
,
'item_id'
=>
4
];
$orderItem
=
OrderItem
::
find
(
$pk
);
$orderItem
=
OrderItem
::
find
One
(
$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
::
find
One
(
$pk
));
$this
->
assertNotNull
(
OrderItem
::
find
One
([
'order_id'
=>
2
,
'item_id'
=>
10
]));
}
public
function
testFilterWhere
()
...
...
tests/unit/extensions/sphinx/ActiveRecordTest.php
View file @
75154d35
...
...
@@ -41,16 +41,16 @@ class ActiveRecordTest extends SphinxTestCase
$this
->
assertTrue
(
$articles
[
1
]
instanceof
ArticleIndex
);
// find fulltext
$article
=
ArticleIndex
::
find
(
2
);
$article
=
ArticleIndex
::
find
One
(
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
::
find
One
([
'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
::
find
One
([
'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
::
find
One
(
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
::
find
One
([
'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
::
find
One
(
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
::
find
One
(
$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
::
find
One
(
2
);
$record
->
delete
();
$record
=
RuntimeIndex
::
find
(
2
);
$record
=
RuntimeIndex
::
find
One
(
2
);
$this
->
assertNull
(
$record
);
// deleteAll
...
...
tests/unit/extensions/sphinx/ActiveRelationTest.php
View file @
75154d35
...
...
@@ -24,7 +24,7 @@ class ActiveRelationTest extends SphinxTestCase
public
function
testFindLazy
()
{
/** @var ArticleDb $article */
$article
=
ArticleDb
::
find
([
'id'
=>
2
]);
$article
=
ArticleDb
::
find
One
([
'id'
=>
2
]);
$this
->
assertFalse
(
$article
->
isRelationPopulated
(
'index'
));
$index
=
$article
->
index
;
$this
->
assertTrue
(
$article
->
isRelationPopulated
(
'index'
));
...
...
tests/unit/extensions/sphinx/ExternalActiveRelationTest.php
View file @
75154d35
...
...
@@ -24,7 +24,7 @@ class ExternalActiveRelationTest extends SphinxTestCase
public
function
testFindLazy
()
{
/** @var ArticleIndex $article */
$article
=
ArticleIndex
::
find
([
'id'
=>
2
]);
$article
=
ArticleIndex
::
find
One
([
'id'
=>
2
]);
// has one :
$this
->
assertFalse
(
$article
->
isRelationPopulated
(
'source'
));
...
...
tests/unit/framework/ar/ActiveRecordTestTrait.php
View file @
75154d35
This diff is collapsed.
Click to expand it.
tests/unit/framework/data/ActiveDataProviderTest.php
View file @
75154d35
...
...
@@ -55,7 +55,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
public
function
testActiveRelation
()
{
/** @var Customer $customer */
$customer
=
Customer
::
find
(
2
);
$customer
=
Customer
::
find
One
(
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
::
find
One
(
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
::
find
One
(
1
);
$provider
=
new
ActiveDataProvider
([
'query'
=>
$order
->
getBooks
(),
]);
...
...
tests/unit/framework/db/ActiveRecordTest.php
View file @
75154d35
...
...
@@ -101,13 +101,13 @@ class ActiveRecordTest extends DatabaseTestCase
public
function
testFindLazyViaTable
()
{
/** @var Order $order */
$order
=
Order
::
find
(
1
);
$order
=
Order
::
find
One
(
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
::
find
One
(
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
::
find
One
(
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
::
find
One
(
1
);
$this
->
assertEquals
(
2
,
count
(
$order
->
books2
));
$order
=
Order
::
find
(
2
);
$order
=
Order
::
find
One
(
2
);
$this
->
assertEquals
(
0
,
count
(
$order
->
books2
));
$order
=
Order
::
find
(
3
);
$order
=
Order
::
find
One
(
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
::
find
One
(
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
::
find
One
(
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
::
find
One
(
2
);
$orders
=
$customer
->
getOrders2
()
->
all
();
$this
->
assertTrue
(
count
(
$orders
)
===
2
);
$this
->
assertTrue
(
$customer
->
orders2
[
0
]
->
customer2
===
$customer
);
...
...
tests/unit/framework/validators/ExistValidatorTest.php
View file @
75154d35
...
...
@@ -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
::
find
One
([
'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
::
find
One
(
1
);
$val
->
validateAttribute
(
$m
,
'id'
);
$this
->
assertFalse
(
$m
->
hasErrors
(
'id'
));
$m
=
Order
::
find
(
1
);
$m
=
Order
::
find
One
(
1
);
$m
->
id
=
10
;
$val
->
validateAttribute
(
$m
,
'id'
);
$this
->
assertTrue
(
$m
->
hasErrors
(
'id'
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment