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
4d049fd9
Commit
4d049fd9
authored
Jan 04, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AR WIP
parent
211c3fb8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
24 deletions
+51
-24
ActiveQuery.php
framework/db/ar/ActiveQuery.php
+3
-1
ActiveRelation.php
framework/db/ar/ActiveRelation.php
+46
-22
Order.php
tests/unit/data/ar/Order.php
+2
-1
No files found.
framework/db/ar/ActiveQuery.php
View file @
4d049fd9
...
...
@@ -95,7 +95,9 @@ class ActiveQuery extends BaseQuery
$class
=
$this
->
modelClass
;
$model
=
$class
::
create
(
$row
);
if
(
!
empty
(
$this
->
with
))
{
$this
->
fetchRelatedModels
(
array
(
$model
),
$this
->
with
);
$models
=
array
(
$model
);
$this
->
fetchRelatedModels
(
$models
,
$this
->
with
);
$model
=
$models
[
0
];
}
return
$model
;
}
...
...
framework/db/ar/ActiveRelation.php
View file @
4d049fd9
...
...
@@ -39,9 +39,25 @@ class ActiveRelation extends ActiveQuery
*/
public
$link
;
/**
* @var
ActiveRelation
* @var
array
*/
public
$via
;
/**
* @var array
*/
public
$viaTable
;
public
function
via
(
$modelClass
,
$properties
=
array
())
{
$this
->
via
=
array
(
$modelClass
,
$properties
);
return
$this
;
}
public
function
viaTable
(
$tableName
,
$link
,
$properties
=
array
())
{
$this
->
viaTable
=
array
(
$tableName
,
$link
,
$properties
);
return
$this
;
}
public
function
createCommand
(
$db
=
null
)
{
...
...
@@ -53,9 +69,13 @@ class ActiveRelation extends ActiveQuery
public
function
findWith
(
$name
,
&
$primaryModels
)
{
if
(
empty
(
$this
->
link
)
||
!
is_array
(
$this
->
link
))
{
if
(
!
is_array
(
$this
->
link
))
{
throw
new
\yii\base\Exception
(
'invalid link'
);
}
if
(
$this
->
via
!==
null
)
{
}
$this
->
filterByPrimaryModels
(
$primaryModels
);
if
(
count
(
$primaryModels
)
===
1
&&
!
$this
->
multiple
)
{
...
...
@@ -64,28 +84,32 @@ class ActiveRelation extends ActiveQuery
}
}
else
{
$models
=
$this
->
all
();
// distribute models into buckets which are indexed by the link keys
$buckets
=
array
();
foreach
(
$models
as
$i
=>
$model
)
{
$key
=
$this
->
getModelKey
(
$model
,
array_keys
(
$this
->
link
));
if
(
$this
->
index
!==
null
)
{
$buckets
[
$key
][
$i
]
=
$model
;
}
else
{
$buckets
[
$key
][]
=
$model
;
}
$this
->
bindModels
(
$name
,
$primaryModels
,
$models
);
}
}
protected
function
bindModels
(
$name
,
&
$primaryModels
,
$models
)
{
$buckets
=
array
();
foreach
(
$models
as
$i
=>
$model
)
{
$key
=
$this
->
getModelKey
(
$model
,
array_keys
(
$this
->
link
));
if
(
$this
->
index
!==
null
)
{
$buckets
[
$key
][
$i
]
=
$model
;
}
else
{
$buckets
[
$key
][]
=
$model
;
}
if
(
!
$this
->
multiple
)
{
foreach
(
$buckets
as
$i
=>
$bucket
)
{
$buckets
[
$i
]
=
reset
(
$bucket
);
}
}
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
;
}
}
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
;
}
}
}
...
...
tests/unit/data/ar/Order.php
View file @
4d049fd9
...
...
@@ -29,7 +29,7 @@ class Order extends ActiveRecord
public
function
books
()
{
return
$this
->
hasMany
(
'Item'
,
array
(
'id'
=>
'item_id'
))
->
via
(
'tbl_order_item'
,
array
(
'order_id'
=>
'id'
))
->
via
Table
(
'tbl_order_item'
,
array
(
'order_id'
=>
'id'
))
->
where
(
array
(
'category_id'
=>
1
));
}
}
\ No newline at end of file
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