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
a7ecbfee
Commit
a7ecbfee
authored
Jan 04, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixing AR issues.
parent
30c52fad
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
103 additions
and
54 deletions
+103
-54
ActiveQuery.php
framework/db/ar/ActiveQuery.php
+12
-1
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+14
-8
ActiveRelation.php
framework/db/ar/ActiveRelation.php
+18
-18
Command.php
framework/db/dao/Command.php
+1
-1
QueryBuilder.php
framework/db/dao/QueryBuilder.php
+7
-2
ActiveRecordTest.php
tests/unit/framework/db/ar/ActiveRecordTest.php
+51
-24
No files found.
framework/db/ar/ActiveQuery.php
View file @
a7ecbfee
...
@@ -214,6 +214,11 @@ class ActiveQuery extends BaseQuery
...
@@ -214,6 +214,11 @@ class ActiveQuery extends BaseQuery
// todo: normalize $relations
// todo: normalize $relations
$primaryModel
=
new
$this
->
modelClass
;
$primaryModel
=
new
$this
->
modelClass
;
foreach
(
$relations
as
$name
=>
$properties
)
{
foreach
(
$relations
as
$name
=>
$properties
)
{
if
(
is_integer
(
$name
))
{
$name
=
$properties
;
$properties
=
array
();
}
if
(
!
method_exists
(
$primaryModel
,
$name
))
{
if
(
!
method_exists
(
$primaryModel
,
$name
))
{
throw
new
Exception
(
"Unknown relation:
$name
"
);
throw
new
Exception
(
"Unknown relation:
$name
"
);
}
}
...
@@ -227,7 +232,13 @@ class ActiveQuery extends BaseQuery
...
@@ -227,7 +232,13 @@ class ActiveQuery extends BaseQuery
// inherit asArray from primary query
// inherit asArray from primary query
$relation
->
asArray
=
$this
->
asArray
;
$relation
->
asArray
=
$this
->
asArray
;
}
}
$relation
->
findWith
(
$name
,
$models
);
if
(
$relation
->
via
!==
null
)
{
$viaName
=
$relation
->
via
;
$viaQuery
=
$primaryModel
->
$viaName
();
$relation
->
findWith
(
$name
,
$models
,
$viaQuery
);
}
else
{
$relation
->
findWith
(
$name
,
$models
);
}
}
}
}
}
}
}
framework/db/ar/ActiveRecord.php
View file @
a7ecbfee
...
@@ -172,7 +172,8 @@ abstract class ActiveRecord extends Model
...
@@ -172,7 +172,8 @@ abstract class ActiveRecord extends Model
}
}
}
elseif
(
$q
!==
null
)
{
}
elseif
(
$q
!==
null
)
{
$query
->
select
=
array
(
$q
);
$query
->
select
=
array
(
$q
);
}
elseif
(
$query
->
select
===
null
)
{
}
if
(
$query
->
select
===
null
)
{
$query
->
select
=
array
(
'COUNT(*)'
);
$query
->
select
=
array
(
'COUNT(*)'
);
}
}
return
$query
;
return
$query
;
...
@@ -300,7 +301,7 @@ abstract class ActiveRecord extends Model
...
@@ -300,7 +301,7 @@ abstract class ActiveRecord extends Model
return
null
;
return
null
;
}
elseif
(
method_exists
(
$this
,
$name
))
{
}
elseif
(
method_exists
(
$this
,
$name
))
{
// related records
// related records
if
(
isset
(
$this
->
_related
[
$name
])
||
array_key_exists
(
$name
,
$this
->
_related
))
{
if
(
isset
(
$this
->
_related
[
$name
])
||
isset
(
$this
->
_related
)
&&
array_key_exists
(
$name
,
$this
->
_related
))
{
return
$this
->
_related
[
$name
];
return
$this
->
_related
[
$name
];
}
else
{
}
else
{
// lazy loading related records
// lazy loading related records
...
@@ -379,8 +380,15 @@ abstract class ActiveRecord extends Model
...
@@ -379,8 +380,15 @@ abstract class ActiveRecord extends Model
*/
*/
public
function
hasOne
(
$class
,
$link
,
$properties
=
array
())
public
function
hasOne
(
$class
,
$link
,
$properties
=
array
())
{
{
$properties
[
'primaryModel'
]
=
$this
;
if
(
strpos
(
$class
,
'\\'
)
===
false
)
{
$primaryClass
=
get_class
(
$this
);
if
(
strpos
(
$primaryClass
,
'\\'
)
!==
false
)
{
$class
=
dirname
(
$primaryClass
)
.
'\\'
.
$class
;
}
}
$properties
[
'modelClass'
]
=
$class
;
$properties
[
'modelClass'
]
=
$class
;
$properties
[
'primaryModel'
]
=
$this
;
$properties
[
'link'
]
=
$link
;
$properties
[
'link'
]
=
$link
;
$properties
[
'multiple'
]
=
false
;
$properties
[
'multiple'
]
=
false
;
return
new
ActiveRelation
(
$properties
);
return
new
ActiveRelation
(
$properties
);
...
@@ -400,11 +408,9 @@ abstract class ActiveRecord extends Model
...
@@ -400,11 +408,9 @@ abstract class ActiveRecord extends Model
*/
*/
public
function
hasMany
(
$class
,
$link
,
$properties
=
array
())
public
function
hasMany
(
$class
,
$link
,
$properties
=
array
())
{
{
$properties
[
'primaryModel'
]
=
$this
;
$relation
=
$this
->
hasOne
(
$class
,
$link
,
$properties
);
$properties
[
'modelClass'
]
=
$class
;
$relation
->
multiple
=
true
;
$properties
[
'link'
]
=
$link
;
return
$relation
;
$properties
[
'multiple'
]
=
true
;
return
new
ActiveRelation
(
$properties
);
}
}
/**
/**
...
...
framework/db/ar/ActiveRelation.php
View file @
a7ecbfee
...
@@ -49,7 +49,7 @@ class ActiveRelation extends ActiveQuery
...
@@ -49,7 +49,7 @@ class ActiveRelation extends ActiveQuery
public
function
via
(
$modelClass
,
$properties
=
array
())
public
function
via
(
$modelClass
,
$properties
=
array
())
{
{
$this
->
via
=
array
(
$modelClass
,
$properties
)
;
$this
->
via
=
$modelClass
;
return
$this
;
return
$this
;
}
}
...
@@ -65,9 +65,12 @@ class ActiveRelation extends ActiveQuery
...
@@ -65,9 +65,12 @@ class ActiveRelation extends ActiveQuery
if
(
$this
->
via
!==
null
)
{
if
(
$this
->
via
!==
null
)
{
/** @var $viaQuery ActiveRelation */
/** @var $viaQuery ActiveRelation */
$viaName
=
$this
->
via
;
$viaName
=
$this
->
via
;
$viaQuery
=
$this
->
$viaName
();
$viaModels
=
$this
->
primaryModel
->
$viaName
;
$primaryModels
=
array
(
$this
->
primaryModel
);
if
(
$viaModels
===
null
)
{
$viaModels
=
$viaQuery
->
findWith
(
$viaName
,
$primaryModels
);
$viaModels
=
array
();
}
elseif
(
!
is_array
(
$viaModels
))
{
$viaModels
=
array
(
$viaModels
);
}
$this
->
filterByModels
(
$viaModels
);
$this
->
filterByModels
(
$viaModels
);
}
else
{
}
else
{
$this
->
filterByModels
(
array
(
$this
->
primaryModel
));
$this
->
filterByModels
(
array
(
$this
->
primaryModel
));
...
@@ -76,17 +79,14 @@ class ActiveRelation extends ActiveQuery
...
@@ -76,17 +79,14 @@ class ActiveRelation extends ActiveQuery
return
parent
::
createCommand
();
return
parent
::
createCommand
();
}
}
public
function
findWith
(
$name
,
&
$primaryModels
)
public
function
findWith
(
$name
,
&
$primaryModels
,
$viaQuery
=
null
)
{
{
if
(
!
is_array
(
$this
->
link
))
{
if
(
!
is_array
(
$this
->
link
))
{
throw
new
\yii\base\Exception
(
'invalid link'
);
throw
new
\yii\base\Exception
(
'invalid link'
);
}
}
if
(
$this
->
via
!==
null
)
{
if
(
$viaQuery
!==
null
)
{
/** @var $viaQuery ActiveRelation */
$viaModels
=
$viaQuery
->
findWith
(
$this
->
via
,
$primaryModels
);
$viaName
=
$this
->
via
;
$viaQuery
=
$this
->
$viaName
();
$viaModels
=
$viaQuery
->
findWith
(
$viaName
,
$primaryModels
);
$this
->
filterByModels
(
$viaModels
);
$this
->
filterByModels
(
$viaModels
);
}
else
{
}
else
{
$this
->
filterByModels
(
$primaryModels
);
$this
->
filterByModels
(
$primaryModels
);
...
@@ -178,21 +178,21 @@ class ActiveRelation extends ActiveQuery
...
@@ -178,21 +178,21 @@ class ActiveRelation extends ActiveQuery
{
{
$attributes
=
array_keys
(
$this
->
link
);
$attributes
=
array_keys
(
$this
->
link
);
$values
=
array
();
$values
=
array
();
if
(
isset
(
$links
[
1
]))
{
if
(
count
(
$attributes
)
===
1
)
{
// single key
$attribute
=
reset
(
$this
->
link
);
foreach
(
$models
as
$model
)
{
$values
[]
=
$model
[
$attribute
];
}
}
else
{
// composite keys
// composite keys
foreach
(
$models
as
$model
)
{
foreach
(
$models
as
$model
)
{
$v
=
array
();
$v
=
array
();
foreach
(
$this
->
link
as
$attribute
=>
$link
)
{
foreach
(
$this
->
link
as
$attribute
=>
$link
)
{
$v
[
$attribute
]
=
is_array
(
$model
)
?
$model
[
$link
]
:
$model
->
$link
;
$v
[
$attribute
]
=
$model
[
$link
]
;
}
}
$values
[]
=
$v
;
$values
[]
=
$v
;
}
}
}
else
{
// single key
$attribute
=
$this
->
link
[
$links
[
0
]];
foreach
(
$models
as
$model
)
{
$values
[]
=
is_array
(
$model
)
?
$model
[
$attribute
]
:
$model
->
$attribute
;
}
}
}
$this
->
andWhere
(
array
(
'in'
,
$attributes
,
$values
));
$this
->
andWhere
(
array
(
'in'
,
$attributes
,
$values
));
}
}
...
...
framework/db/dao/Command.php
View file @
a7ecbfee
...
@@ -365,7 +365,7 @@ class Command extends \yii\base\Component
...
@@ -365,7 +365,7 @@ class Command extends \yii\base\Component
}
}
\Yii
::
trace
(
"Querying SQL:
{
$sql
}{
$paramLog
}
"
,
__CLASS__
);
\Yii
::
trace
(
"Querying SQL:
{
$sql
}{
$paramLog
}
"
,
__CLASS__
);
echo
$sql
.
"
\n\n
"
;
if
(
$db
->
queryCachingCount
>
0
&&
$db
->
queryCachingDuration
>=
0
&&
$method
!==
''
)
{
if
(
$db
->
queryCachingCount
>
0
&&
$db
->
queryCachingDuration
>=
0
&&
$method
!==
''
)
{
$cache
=
\Yii
::
$application
->
getComponent
(
$db
->
queryCacheID
);
$cache
=
\Yii
::
$application
->
getComponent
(
$db
->
queryCacheID
);
}
}
...
...
framework/db/dao/QueryBuilder.php
View file @
a7ecbfee
...
@@ -568,7 +568,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -568,7 +568,7 @@ class QueryBuilder extends \yii\base\Object
$values
=
(
array
)
$values
;
$values
=
(
array
)
$values
;
if
(
$values
===
array
()
||
$column
===
array
())
{
if
(
$values
===
array
()
||
$column
===
array
())
{
return
$operator
===
'
in
'
?
'0=1'
:
''
;
return
$operator
===
'
IN
'
?
'0=1'
:
''
;
}
}
if
(
is_array
(
$column
))
{
if
(
is_array
(
$column
))
{
...
@@ -593,7 +593,12 @@ class QueryBuilder extends \yii\base\Object
...
@@ -593,7 +593,12 @@ class QueryBuilder extends \yii\base\Object
$column
=
$this
->
quoteColumnName
(
$column
);
$column
=
$this
->
quoteColumnName
(
$column
);
}
}
return
"
$column
$operator
("
.
implode
(
', '
,
$values
)
.
')'
;
if
(
count
(
$values
)
>
1
)
{
return
"
$column
$operator
("
.
implode
(
', '
,
$values
)
.
')'
;
}
else
{
$operator
=
$operator
===
'IN'
?
'='
:
'<>'
;
return
"
$column$operator
{
$values
[
0
]
}
"
;
}
}
}
protected
function
buildCompositeInCondition
(
$operator
,
$columns
,
$values
)
protected
function
buildCompositeInCondition
(
$operator
,
$columns
,
$values
)
...
...
tests/unit/framework/db/ar/ActiveRecordTest.php
View file @
a7ecbfee
...
@@ -52,11 +52,11 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -52,11 +52,11 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
// find count
// find count
// $this->assertEquals(3, Customer::count
());
$this
->
assertEquals
(
3
,
Customer
::
count
()
->
value
());
//
$this->assertEquals(2, Customer::count(array(
$this
->
assertEquals
(
2
,
Customer
::
count
(
array
(
//
'where' => 'id=1 OR id=2',
'where'
=>
'id=1 OR id=2'
,
// )
));
))
->
value
(
));
//
$this->assertEquals(2, Customer::find()->select('COUNT(*)')->where('id=1 OR id=2')->value());
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
select
(
'COUNT(*)'
)
->
where
(
'id=1 OR id=2'
)
->
value
());
}
}
public
function
testFindBySql
()
public
function
testFindBySql
()
...
@@ -86,25 +86,52 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -86,25 +86,52 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$customers
=
Customer
::
find
()
->
active
()
->
all
();
$customers
=
Customer
::
find
()
->
active
()
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
$this
->
assertEquals
(
2
,
count
(
$customers
));
}
}
//
// public function testFindLazy()
public
function
testFindLazy
()
// {
{
// $customer = Customer::find(2);
/** @var $customer Customer */
// $orders = $customer->orders;
$customer
=
Customer
::
find
(
2
);
// $this->assertEquals(2, count($orders));
$orders
=
$customer
->
orders
;
//
$this
->
assertEquals
(
2
,
count
(
$orders
));
// $orders = $customer->orders()->where('id=3')->all();
// $this->assertEquals(1, count($orders));
$orders
=
$customer
->
orders
()
->
where
(
'id=3'
)
->
all
();
// $this->assertEquals(3, $orders[0]->id);
$this
->
assertEquals
(
1
,
count
(
$orders
));
// }
$this
->
assertEquals
(
3
,
$orders
[
0
]
->
id
);
//
}
// public function testFindEager()
// {
public
function
testFindEager
()
// $customers = Customer::find()->with('orders')->all();
{
// $this->assertEquals(3, count($customers));
$customers
=
Customer
::
find
()
->
with
(
'orders'
)
->
all
();
// $this->assertEquals(1, count($customers[0]->orders));
$this
->
assertEquals
(
3
,
count
(
$customers
));
// $this->assertEquals(2, count($customers[1]->orders));
$this
->
assertEquals
(
1
,
count
(
$customers
[
0
]
->
orders
));
// }
$this
->
assertEquals
(
2
,
count
(
$customers
[
1
]
->
orders
));
}
public
function
testFindLazyVia
()
{
/** @var $order Order */
$order
=
Order
::
find
(
1
);
$this
->
assertEquals
(
1
,
$order
->
id
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
id
);
$order
=
Order
::
find
(
1
);
$order
->
id
=
100
;
$this
->
assertEquals
(
array
(),
$order
->
items
);
}
public
function
testFindEagerVia
()
{
$orders
=
Order
::
find
()
->
with
(
'items'
)
->
orderBy
(
'id'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$orders
));
$order
=
$orders
[
0
];
$this
->
assertEquals
(
1
,
$order
->
id
);
$this
->
assertEquals
(
2
,
count
(
$order
->
items
));
$this
->
assertEquals
(
1
,
$order
->
items
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$order
->
items
[
1
]
->
id
);
}
// public function testInsert()
// public function testInsert()
// {
// {
...
...
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