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
30c52fad
Commit
30c52fad
authored
Jan 04, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finished draft implementation of new AR.
parent
4d049fd9
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
101 additions
and
82 deletions
+101
-82
ActiveQuery.php
framework/db/ar/ActiveQuery.php
+12
-17
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+1
-1
ActiveRelation.php
framework/db/ar/ActiveRelation.php
+66
-20
TestCase.php
framework/test/TestCase.php
+2
-0
Customer.php
tests/unit/data/ar/Customer.php
+1
-1
Order.php
tests/unit/data/ar/Order.php
+3
-2
ObjectTest.php
tests/unit/framework/base/ObjectTest.php
+16
-41
No files found.
framework/db/ar/ActiveQuery.php
View file @
30c52fad
...
...
@@ -66,14 +66,15 @@ class ActiveQuery extends BaseQuery
{
$command
=
$this
->
createCommand
();
$rows
=
$command
->
queryAll
();
if
(
$rows
===
array
())
{
if
(
$rows
!==
array
())
{
$models
=
$this
->
createModels
(
$rows
);
if
(
!
empty
(
$this
->
with
))
{
$this
->
fetchRelatedModels
(
$models
,
$this
->
with
);
}
return
$models
;
}
else
{
return
array
();
}
$models
=
$this
->
createModels
(
$rows
);
if
(
!
empty
(
$this
->
with
))
{
$this
->
fetchRelatedModels
(
$models
,
$this
->
with
);
}
return
$models
;
}
/**
...
...
@@ -86,11 +87,7 @@ class ActiveQuery extends BaseQuery
{
$command
=
$this
->
createCommand
();
$row
=
$command
->
queryRow
();
if
(
$row
===
false
)
{
return
false
;
}
elseif
(
$this
->
asArray
)
{
return
$row
;
}
else
{
if
(
$row
!==
false
&&
!
$this
->
asArray
)
{
/** @var $class ActiveRecord */
$class
=
$this
->
modelClass
;
$model
=
$class
::
create
(
$row
);
...
...
@@ -100,6 +97,8 @@ class ActiveQuery extends BaseQuery
$model
=
$models
[
0
];
}
return
$model
;
}
else
{
return
$row
===
false
?
null
:
$row
;
}
}
...
...
@@ -126,17 +125,13 @@ class ActiveQuery extends BaseQuery
/**
* Creates a DB command that can be used to execute this query.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return Command the created DB command instance.
*/
public
function
createCommand
(
$db
=
null
)
public
function
createCommand
()
{
/** @var $modelClass ActiveRecord */
$modelClass
=
$this
->
modelClass
;
if
(
$db
===
null
)
{
$db
=
$modelClass
::
getDbConnection
();
}
$db
=
$modelClass
::
getDbConnection
();
if
(
$this
->
sql
===
null
)
{
if
(
$this
->
from
===
null
)
{
$tableName
=
$modelClass
::
tableName
();
...
...
framework/db/ar/ActiveRecord.php
View file @
30c52fad
...
...
@@ -878,7 +878,7 @@ abstract class ActiveRecord extends Model
public
static
function
create
(
$row
)
{
$record
=
static
::
instantiate
(
$row
);
$columns
=
static
::
model
()
->
getTableSchema
()
->
columns
;
$columns
=
static
::
getTableSchema
()
->
columns
;
foreach
(
$row
as
$name
=>
$value
)
{
if
(
isset
(
$columns
[
$name
]))
{
$record
->
_attributes
[
$name
]
=
$value
;
...
...
framework/db/ar/ActiveRelation.php
View file @
30c52fad
...
...
@@ -59,12 +59,21 @@ class ActiveRelation extends ActiveQuery
return
$this
;
}
public
function
createCommand
(
$db
=
null
)
public
function
createCommand
()
{
if
(
$this
->
primaryModel
!==
null
)
{
$this
->
filterByPrimaryModels
(
array
(
$this
->
primaryModel
));
if
(
$this
->
via
!==
null
)
{
/** @var $viaQuery ActiveRelation */
$viaName
=
$this
->
via
;
$viaQuery
=
$this
->
$viaName
();
$primaryModels
=
array
(
$this
->
primaryModel
);
$viaModels
=
$viaQuery
->
findWith
(
$viaName
,
$primaryModels
);
$this
->
filterByModels
(
$viaModels
);
}
else
{
$this
->
filterByModels
(
array
(
$this
->
primaryModel
));
}
}
return
parent
::
createCommand
(
$db
);
return
parent
::
createCommand
();
}
public
function
findWith
(
$name
,
&
$primaryModels
)
...
...
@@ -74,44 +83,81 @@ class ActiveRelation extends ActiveQuery
}
if
(
$this
->
via
!==
null
)
{
/** @var $viaQuery ActiveRelation */
$viaName
=
$this
->
via
;
$viaQuery
=
$this
->
$viaName
();
$viaModels
=
$viaQuery
->
findWith
(
$viaName
,
$primaryModels
);
$this
->
filterByModels
(
$viaModels
);
}
else
{
$this
->
filterByModels
(
$primaryModels
);
}
$this
->
filterByPrimaryModels
(
$primaryModels
);
if
(
count
(
$primaryModels
)
===
1
&&
!
$this
->
multiple
)
{
$model
=
$this
->
one
();
foreach
(
$primaryModels
as
$i
=>
$primaryModel
)
{
$primaryModels
[
$i
][
$name
]
=
$
this
->
one
()
;
$primaryModels
[
$i
][
$name
]
=
$
model
;
}
return
array
(
$model
);
}
else
{
$models
=
$this
->
all
();
$this
->
bindModels
(
$name
,
$primaryModels
,
$models
);
if
(
isset
(
$viaModels
,
$viaQuery
))
{
$buckets
=
$this
->
buildBuckets
(
$models
,
$this
->
link
,
$viaModels
,
$viaQuery
->
link
);
}
else
{
$buckets
=
$this
->
buildBuckets
(
$models
,
$this
->
link
);
}
foreach
(
$primaryModels
as
$i
=>
$primaryModel
)
{
if
(
isset
(
$viaQuery
))
{
$key
=
$this
->
getModelKey
(
$primaryModel
,
array_values
(
$viaQuery
->
link
));
}
else
{
$key
=
$this
->
getModelKey
(
$primaryModel
,
array_values
(
$this
->
link
));
}
if
(
isset
(
$buckets
[
$key
]))
{
$primaryModels
[
$i
][
$name
]
=
$buckets
[
$key
];
}
else
{
$primaryModels
[
$i
][
$name
]
=
$this
->
multiple
?
array
()
:
null
;
}
}
return
$models
;
}
}
protected
function
b
indModels
(
$name
,
&
$primaryModels
,
$models
)
protected
function
b
uildBuckets
(
$models
,
$link
,
$viaModels
=
null
,
$viaLink
=
null
)
{
$buckets
=
array
();
foreach
(
$models
as
$i
=>
$model
)
{
$key
=
$this
->
getModelKey
(
$model
,
array_keys
(
$
this
->
link
));
$key
=
$this
->
getModelKey
(
$model
,
array_keys
(
$link
));
if
(
$this
->
index
!==
null
)
{
$buckets
[
$key
][
$i
]
=
$model
;
}
else
{
$buckets
[
$key
][]
=
$model
;
}
}
if
(
$viaModels
!==
null
)
{
$viaBuckets
=
array
();
foreach
(
$viaModels
as
$viaModel
)
{
$key1
=
$this
->
getModelKey
(
$viaModel
,
array_keys
(
$viaLink
));
$key2
=
$this
->
getModelKey
(
$viaModel
,
array_values
(
$link
));
if
(
isset
(
$buckets
[
$key2
]))
{
foreach
(
$buckets
[
$key2
]
as
$i
=>
$bucket
)
{
if
(
$this
->
index
!==
null
)
{
$viaBuckets
[
$key1
][
$i
]
=
$bucket
;
}
else
{
$viaBuckets
[
$key1
][]
=
$bucket
;
}
}
}
}
$buckets
=
$viaBuckets
;
}
if
(
!
$this
->
multiple
)
{
foreach
(
$buckets
as
$i
=>
$bucket
)
{
$buckets
[
$i
]
=
reset
(
$bucket
);
}
}
foreach
(
$primaryModels
as
$i
=>
$primaryModel
)
{
$key
=
$this
->
getModelKey
(
$primaryModel
,
array_values
(
$this
->
link
));
if
(
isset
(
$buckets
[
$key
]))
{
$primaryModels
[
$i
][
$name
]
=
$buckets
[
$key
];
}
else
{
$primaryModels
[
$i
][
$name
]
=
$this
->
multiple
?
array
()
:
null
;
}
}
return
$buckets
;
}
protected
function
getModelKey
(
$model
,
$attributes
)
...
...
@@ -128,13 +174,13 @@ class ActiveRelation extends ActiveQuery
}
}
protected
function
filterBy
PrimaryModels
(
$primaryM
odels
)
protected
function
filterBy
Models
(
$m
odels
)
{
$attributes
=
array_keys
(
$this
->
link
);
$values
=
array
();
if
(
isset
(
$links
[
1
]))
{
// composite keys
foreach
(
$
primaryM
odels
as
$model
)
{
foreach
(
$
m
odels
as
$model
)
{
$v
=
array
();
foreach
(
$this
->
link
as
$attribute
=>
$link
)
{
$v
[
$attribute
]
=
is_array
(
$model
)
?
$model
[
$link
]
:
$model
->
$link
;
...
...
@@ -144,7 +190,7 @@ class ActiveRelation extends ActiveQuery
}
else
{
// single key
$attribute
=
$this
->
link
[
$links
[
0
]];
foreach
(
$
primaryM
odels
as
$model
)
{
foreach
(
$
m
odels
as
$model
)
{
$values
[]
=
is_array
(
$model
)
?
$model
[
$attribute
]
:
$model
->
$attribute
;
}
}
...
...
framework/test/TestCase.php
View file @
30c52fad
...
...
@@ -10,7 +10,9 @@
namespace
yii\test
;
require_once
(
'PHPUnit/Runner/Version.php'
);
spl_autoload_unregister
(
array
(
'YiiBase'
,
'autoload'
));
require_once
(
'PHPUnit/Autoload.php'
);
spl_autoload_register
(
array
(
'YiiBase'
,
'autoload'
));
// put yii's autoloader at the end
/**
* TestCase is the base class for all test case classes.
...
...
tests/unit/data/ar/Customer.php
View file @
30c52fad
...
...
@@ -22,7 +22,7 @@ class Customer extends ActiveRecord
* @param ActiveQuery $query
* @return ActiveQuery
*/
public
function
active
(
$query
)
public
static
function
active
(
$query
)
{
return
$query
->
andWhere
(
array
(
'status'
=>
self
::
STATUS_ACTIVE
));
}
...
...
tests/unit/data/ar/Order.php
View file @
30c52fad
...
...
@@ -22,8 +22,9 @@ class Order extends ActiveRecord
public
function
items
()
{
return
$this
->
hasMany
(
'Item'
,
array
(
'id'
=>
'item_id'
))
->
via
(
'orderItems'
)
->
orderBy
(
'id'
);
->
via
(
'orderItems'
,
function
(
$q
)
{
// additional query configuration
})
->
orderBy
(
'id'
);
}
public
function
books
()
...
...
tests/unit/framework/base/ObjectTest.php
View file @
30c52fad
...
...
@@ -7,7 +7,7 @@ class Foo extends \yii\base\Object
public
$prop
;
}
class
Bar
extends
\yii\base\Component
implements
\yii\base\Initable
class
Bar
extends
\yii\base\Component
{
public
$prop1
;
public
$prop2
;
...
...
@@ -41,30 +41,6 @@ class ObjectTest extends \yiiunit\TestCase
$this
->
object
=
null
;
}
public
function
testNewInstance
()
{
$foo
=
Foo
::
newInstance
(
array
(
'prop'
=>
array
(
'test'
=>
'test'
,
),
));
$this
->
assertEquals
(
'test'
,
$foo
->
prop
[
'test'
]);
$bar
=
Bar
::
newInstance
(
array
(),
10
,
20
);
$this
->
assertEquals
(
30
,
$bar
->
prop1
);
$this
->
assertEquals
(
null
,
$bar
->
prop2
);
$this
->
assertEquals
(
3
,
$bar
->
prop3
);
$bar
=
Bar
::
newInstance
(
array
(
'prop2'
=>
'x'
,
'prop3'
=>
400
,
),
100
,
200
);
$this
->
assertEquals
(
300
,
$bar
->
prop1
);
$this
->
assertEquals
(
'x'
,
$bar
->
prop2
);
$this
->
assertEquals
(
3
,
$bar
->
prop3
);
}
public
function
testHasProperty
()
{
$this
->
assertTrue
(
$this
->
object
->
hasProperty
(
'Text'
),
"Component hasn't property Text"
);
...
...
@@ -88,19 +64,19 @@ class ObjectTest extends \yiiunit\TestCase
public
function
testGetProperty
()
{
$this
->
assertTrue
(
'default'
===
$this
->
object
->
Text
);
$this
->
setExpectedException
(
'yii\base\Exception'
);
$value2
=
$this
->
object
->
Caption
;
$this
->
assertTrue
(
'default'
===
$this
->
object
->
Text
);
$this
->
setExpectedException
(
'yii\base\
BadProperty
Exception'
);
$value2
=
$this
->
object
->
Caption
;
}
public
function
testSetProperty
()
{
$value
=
'new value'
;
$this
->
object
->
Text
=
$value
;
$text
=
$this
->
object
->
Text
;
$this
->
assertTrue
(
$value
===
$this
->
object
->
Text
);
$this
->
setExpectedException
(
'yii\base\Exception'
);
$this
->
object
->
NewMember
=
$value
;
$value
=
'new value'
;
$this
->
object
->
Text
=
$value
;
$text
=
$this
->
object
->
Text
;
$this
->
assertTrue
(
$value
===
$this
->
object
->
Text
);
$this
->
setExpectedException
(
'yii\base\
BadProperty
Exception'
);
$this
->
object
->
NewMember
=
$value
;
}
public
function
testIsset
()
...
...
@@ -112,7 +88,7 @@ class ObjectTest extends \yiiunit\TestCase
$this
->
assertFalse
(
isset
(
$this
->
object
->
Text
));
$this
->
assertFalse
(
!
empty
(
$this
->
object
->
Text
));
$this
->
object
->
Text
=
''
;
$this
->
object
->
Text
=
''
;
$this
->
assertTrue
(
isset
(
$this
->
object
->
Text
));
$this
->
assertTrue
(
empty
(
$this
->
object
->
Text
));
}
...
...
@@ -131,20 +107,19 @@ class NewObject extends \yii\base\Component
public
function
setText
(
$value
)
{
$this
->
_text
=
$value
;
$this
->
_text
=
$value
;
}
public
function
getObject
()
{
if
(
!
$this
->
_object
)
{
$this
->
_object
=
new
self
;
$this
->
_object
->
_text
=
'object text'
;
if
(
!
$this
->
_object
)
{
$this
->
_object
=
new
self
;
$this
->
_object
->
_text
=
'object text'
;
}
return
$this
->
_object
;
}
public
function
exprEvaluator
(
$p1
,
$comp
)
public
function
exprEvaluator
(
$p1
,
$comp
)
{
return
"Hello
$p1
"
;
}
...
...
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