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
c9b104f0
Commit
c9b104f0
authored
Jan 09, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored AR relation.
parent
0e3e2698
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
71 additions
and
76 deletions
+71
-76
ActiveQuery.php
framework/db/ActiveQuery.php
+12
-7
ActiveRecord.php
framework/db/ActiveRecord.php
+25
-41
ActiveRelation.php
framework/db/ActiveRelation.php
+26
-20
Customer.php
tests/unit/data/ar/Customer.php
+1
-1
Order.php
tests/unit/data/ar/Order.php
+4
-4
OrderItem.php
tests/unit/data/ar/OrderItem.php
+2
-2
ActiveRecordTest.php
tests/unit/framework/db/ActiveRecordTest.php
+1
-1
No files found.
framework/db/ActiveQuery.php
View file @
c9b104f0
...
@@ -251,16 +251,21 @@ class ActiveQuery extends Query
...
@@ -251,16 +251,21 @@ class ActiveQuery extends Query
$childName
=
null
;
$childName
=
null
;
}
}
if
(
!
isset
(
$relations
[
$name
]))
{
$t
=
strtolower
(
$name
);
if
(
!
method_exists
(
$model
,
$name
))
{
if
(
!
isset
(
$relations
[
$t
]))
{
$getter
=
'get'
.
$name
;
if
(
!
method_exists
(
$model
,
$getter
))
{
throw
new
Exception
(
"Unknown relation:
$name
"
);
}
$relation
=
$model
->
$getter
();
if
(
$relation
instanceof
ActiveRelation
)
{
$relation
->
primaryModel
=
null
;
$relations
[
$t
]
=
$relation
;
}
else
{
throw
new
Exception
(
"Unknown relation:
$name
"
);
throw
new
Exception
(
"Unknown relation:
$name
"
);
}
}
/** @var $relation ActiveRelation */
$relation
=
$model
->
$name
();
$relation
->
primaryModel
=
null
;
$relations
[
$name
]
=
$relation
;
}
else
{
}
else
{
$relation
=
$relations
[
$
name
];
$relation
=
$relations
[
$
t
];
}
}
if
(
isset
(
$childName
))
{
if
(
isset
(
$childName
))
{
...
...
framework/db/ActiveRecord.php
View file @
c9b104f0
...
@@ -284,18 +284,17 @@ abstract class ActiveRecord extends Model
...
@@ -284,18 +284,17 @@ abstract class ActiveRecord extends Model
return
$this
->
_attributes
[
$name
];
return
$this
->
_attributes
[
$name
];
}
elseif
(
isset
(
$this
->
getTableSchema
()
->
columns
[
$name
]))
{
}
elseif
(
isset
(
$this
->
getTableSchema
()
->
columns
[
$name
]))
{
return
null
;
return
null
;
}
elseif
(
method_exists
(
$this
,
$name
))
{
}
else
{
// related records
$t
=
strtolower
(
$name
);
if
(
isset
(
$this
->
_related
[
$name
])
||
isset
(
$this
->
_related
)
&&
array_key_exists
(
$name
,
$this
->
_related
))
{
if
(
isset
(
$this
->
_related
[
$t
])
||
$this
->
_related
!==
null
&&
array_key_exists
(
$t
,
$this
->
_related
))
{
return
$this
->
_related
[
$name
];
return
$this
->
_related
[
$t
];
}
$value
=
parent
::
__get
(
$name
);
if
(
$value
instanceof
ActiveRelation
)
{
return
$this
->
_related
[
$t
]
=
$value
->
multiple
?
$value
->
all
()
:
$value
->
one
();
}
else
{
}
else
{
// lazy loading related records
return
$value
;
/** @var $relation ActiveRelation */
$relation
=
$this
->
$name
();
return
$this
->
_related
[
$name
]
=
$relation
->
multiple
?
$relation
->
all
()
:
$relation
->
one
();
}
}
}
else
{
return
parent
::
__get
(
$name
);
}
}
}
}
...
@@ -309,8 +308,6 @@ abstract class ActiveRecord extends Model
...
@@ -309,8 +308,6 @@ abstract class ActiveRecord extends Model
{
{
if
(
isset
(
$this
->
getTableSchema
()
->
columns
[
$name
]))
{
if
(
isset
(
$this
->
getTableSchema
()
->
columns
[
$name
]))
{
$this
->
_attributes
[
$name
]
=
$value
;
$this
->
_attributes
[
$name
]
=
$value
;
}
elseif
(
method_exists
(
$this
,
$name
))
{
$this
->
_related
[
$name
]
=
$value
;
}
else
{
}
else
{
parent
::
__set
(
$name
,
$value
);
parent
::
__set
(
$name
,
$value
);
}
}
...
@@ -325,11 +322,13 @@ abstract class ActiveRecord extends Model
...
@@ -325,11 +322,13 @@ abstract class ActiveRecord extends Model
*/
*/
public
function
__isset
(
$name
)
public
function
__isset
(
$name
)
{
{
if
(
isset
(
$this
->
_attributes
[
$name
])
||
isset
(
$this
->
_related
[
$name
]))
{
if
(
isset
(
$this
->
getTableSchema
()
->
columns
[
$name
]))
{
return
true
;
return
isset
(
$this
->
_related
[
$name
]);
}
elseif
(
isset
(
$this
->
getTableSchema
()
->
columns
[
$name
])
||
method_exists
(
$this
,
$name
))
{
return
false
;
}
else
{
}
else
{
$t
=
strtolower
(
$name
);
if
(
isset
(
$this
->
_related
[
$t
]))
{
return
true
;
}
return
parent
::
__isset
(
$name
);
return
parent
::
__isset
(
$name
);
}
}
}
}
...
@@ -344,10 +343,13 @@ abstract class ActiveRecord extends Model
...
@@ -344,10 +343,13 @@ abstract class ActiveRecord extends Model
{
{
if
(
isset
(
$this
->
getTableSchema
()
->
columns
[
$name
]))
{
if
(
isset
(
$this
->
getTableSchema
()
->
columns
[
$name
]))
{
unset
(
$this
->
_attributes
[
$name
]);
unset
(
$this
->
_attributes
[
$name
]);
}
elseif
(
method_exists
(
$this
,
$name
))
{
unset
(
$this
->
_related
[
$name
]);
}
else
{
}
else
{
parent
::
__unset
(
$name
);
$t
=
strtolower
(
$name
);
if
(
isset
(
$this
->
_related
[
$t
]))
{
unset
(
$this
->
_related
[
$t
]);
}
else
{
parent
::
__unset
(
$name
);
}
}
}
}
}
...
@@ -399,30 +401,12 @@ abstract class ActiveRecord extends Model
...
@@ -399,30 +401,12 @@ abstract class ActiveRecord extends Model
}
}
/**
/**
* Initializes the internal storage for the relation.
* @param string $name
* This method is internally used by [[ActiveQuery]] when populating relation data.
* @param mixed $value
* @param ActiveRelation $relation the relation object
*/
*/
public
function
initRelation
(
$relation
)
public
function
populateRelation
(
$name
,
$value
)
{
{
$this
->
_related
[
$relation
->
name
]
=
$relation
->
hasMany
?
array
()
:
null
;
$this
->
_related
[
$name
]
=
$value
;
}
/**
* @param ActiveRelation $relation
* @param ActiveRecord $record
*/
public
function
addRelatedRecord
(
$relation
,
$record
)
{
if
(
$relation
->
hasMany
)
{
if
(
$relation
->
indexBy
!==
null
)
{
$this
->
_related
[
$relation
->
name
][
$record
->
{
$relation
->
indexBy
}]
=
$record
;
}
else
{
$this
->
_related
[
$relation
->
name
][]
=
$record
;
}
}
else
{
$this
->
_related
[
$relation
->
name
]
=
$record
;
}
}
}
/**
/**
...
...
framework/db/ActiveRelation.php
View file @
c9b104f0
...
@@ -51,17 +51,23 @@ class ActiveRelation extends ActiveQuery
...
@@ -51,17 +51,23 @@ class ActiveRelation extends ActiveQuery
* @param string $relationName
* @param string $relationName
* @param callback $callback
* @param callback $callback
* @return ActiveRelation
* @return ActiveRelation
* @throws Exception
*/
*/
public
function
via
(
$relationName
,
$callback
=
null
)
public
function
via
(
$relationName
,
$callback
=
null
)
{
{
/** @var $relation ActiveRelation */
$getter
=
'get'
.
$relationName
;
$relation
=
$this
->
primaryModel
->
$relationName
();
if
(
method_exists
(
$this
->
primaryModel
,
$getter
))
{
$relation
->
primaryModel
=
null
;
$relation
=
$this
->
primaryModel
->
$getter
();
$this
->
via
=
array
(
$relationName
,
$relation
);
if
(
$relation
instanceof
ActiveRelation
)
{
if
(
$callback
!==
null
)
{
$relation
->
primaryModel
=
null
;
call_user_func
(
$callback
,
$relation
);
$this
->
via
=
array
(
$relationName
,
$relation
);
if
(
$callback
!==
null
)
{
call_user_func
(
$callback
,
$relation
);
}
return
$this
;
}
}
}
return
$this
;
throw
new
Exception
(
'Unknown relation: '
.
$relationName
)
;
}
}
/**
/**
...
@@ -106,9 +112,11 @@ class ActiveRelation extends ActiveQuery
...
@@ -106,9 +112,11 @@ class ActiveRelation extends ActiveQuery
list
(
$viaName
,
$viaQuery
)
=
$this
->
via
;
list
(
$viaName
,
$viaQuery
)
=
$this
->
via
;
$viaQuery
->
primaryModel
=
$this
->
primaryModel
;
$viaQuery
->
primaryModel
=
$this
->
primaryModel
;
if
(
$viaQuery
->
multiple
)
{
if
(
$viaQuery
->
multiple
)
{
$this
->
primaryModel
->
$viaName
=
$viaModels
=
$viaQuery
->
all
();
$viaModels
=
$viaQuery
->
all
();
$this
->
primaryModel
->
populateRelation
(
$viaName
,
$viaModels
);
}
else
{
}
else
{
$this
->
primaryModel
->
$viaName
=
$model
=
$viaQuery
->
one
();
$model
=
$viaQuery
->
one
();
$this
->
primaryModel
->
populateRelation
(
$viaName
,
$model
);
$viaModels
=
$model
===
null
?
array
()
:
array
(
$model
);
$viaModels
=
$model
===
null
?
array
()
:
array
(
$model
);
}
}
$this
->
filterByModels
(
$viaModels
);
$this
->
filterByModels
(
$viaModels
);
...
@@ -144,7 +152,11 @@ class ActiveRelation extends ActiveQuery
...
@@ -144,7 +152,11 @@ class ActiveRelation extends ActiveQuery
if
(
count
(
$primaryModels
)
===
1
&&
!
$this
->
multiple
)
{
if
(
count
(
$primaryModels
)
===
1
&&
!
$this
->
multiple
)
{
$model
=
$this
->
one
();
$model
=
$this
->
one
();
foreach
(
$primaryModels
as
$i
=>
$primaryModel
)
{
foreach
(
$primaryModels
as
$i
=>
$primaryModel
)
{
$primaryModels
[
$i
][
$name
]
=
$model
;
if
(
$primaryModel
instanceof
ActiveRecord
)
{
$primaryModel
->
populateRelation
(
$name
,
$model
);
}
else
{
$primaryModels
[
$i
][
$name
]
=
$model
;
}
}
}
return
array
(
$model
);
return
array
(
$model
);
}
else
{
}
else
{
...
@@ -158,10 +170,11 @@ class ActiveRelation extends ActiveQuery
...
@@ -158,10 +170,11 @@ class ActiveRelation extends ActiveQuery
$link
=
array_values
(
isset
(
$viaQuery
)
?
$viaQuery
->
link
:
$this
->
link
);
$link
=
array_values
(
isset
(
$viaQuery
)
?
$viaQuery
->
link
:
$this
->
link
);
foreach
(
$primaryModels
as
$i
=>
$primaryModel
)
{
foreach
(
$primaryModels
as
$i
=>
$primaryModel
)
{
$key
=
$this
->
getModelKey
(
$primaryModel
,
$link
);
$key
=
$this
->
getModelKey
(
$primaryModel
,
$link
);
if
(
isset
(
$buckets
[
$key
]))
{
$value
=
isset
(
$buckets
[
$key
])
?
$buckets
[
$key
]
:
(
$this
->
multiple
?
array
()
:
null
);
$primaryModels
[
$i
][
$name
]
=
$buckets
[
$key
];
if
(
$primaryModel
instanceof
ActiveRecord
)
{
$primaryModel
->
populateRelation
(
$name
,
$value
);
}
else
{
}
else
{
$primaryModels
[
$i
][
$name
]
=
$
this
->
multiple
?
array
()
:
null
;
$primaryModels
[
$i
][
$name
]
=
$
value
;
}
}
}
}
return
$models
;
return
$models
;
...
@@ -262,11 +275,4 @@ class ActiveRelation extends ActiveQuery
...
@@ -262,11 +275,4 @@ class ActiveRelation extends ActiveQuery
$sql
=
$db
->
getQueryBuilder
()
->
build
(
$this
);
$sql
=
$db
->
getQueryBuilder
()
->
build
(
$this
);
return
$db
->
createCommand
(
$sql
,
$this
->
params
)
->
queryAll
();
return
$db
->
createCommand
(
$sql
,
$this
->
params
)
->
queryAll
();
}
}
public
function
link
(
$model
)
{
/**
* 1. Set models
*/
}
}
}
tests/unit/data/ar/Customer.php
View file @
c9b104f0
...
@@ -13,7 +13,7 @@ class Customer extends ActiveRecord
...
@@ -13,7 +13,7 @@ class Customer extends ActiveRecord
return
'tbl_customer'
;
return
'tbl_customer'
;
}
}
public
function
o
rders
()
public
function
getO
rders
()
{
{
return
$this
->
hasMany
(
'Order'
,
array
(
'customer_id'
=>
'id'
));
return
$this
->
hasMany
(
'Order'
,
array
(
'customer_id'
=>
'id'
));
}
}
...
...
tests/unit/data/ar/Order.php
View file @
c9b104f0
...
@@ -9,17 +9,17 @@ class Order extends ActiveRecord
...
@@ -9,17 +9,17 @@ class Order extends ActiveRecord
return
'tbl_order'
;
return
'tbl_order'
;
}
}
public
function
c
ustomer
()
public
function
getC
ustomer
()
{
{
return
$this
->
hasOne
(
'Customer'
,
array
(
'id'
=>
'customer_id'
));
return
$this
->
hasOne
(
'Customer'
,
array
(
'id'
=>
'customer_id'
));
}
}
public
function
o
rderItems
()
public
function
getO
rderItems
()
{
{
return
$this
->
hasMany
(
'OrderItem'
,
array
(
'order_id'
=>
'id'
));
return
$this
->
hasMany
(
'OrderItem'
,
array
(
'order_id'
=>
'id'
));
}
}
public
function
i
tems
()
public
function
getI
tems
()
{
{
return
$this
->
hasMany
(
'Item'
,
array
(
'id'
=>
'item_id'
))
return
$this
->
hasMany
(
'Item'
,
array
(
'id'
=>
'item_id'
))
->
via
(
'orderItems'
,
function
(
$q
)
{
->
via
(
'orderItems'
,
function
(
$q
)
{
...
@@ -27,7 +27,7 @@ class Order extends ActiveRecord
...
@@ -27,7 +27,7 @@ class Order extends ActiveRecord
})
->
orderBy
(
'id'
);
})
->
orderBy
(
'id'
);
}
}
public
function
b
ooks
()
public
function
getB
ooks
()
{
{
return
$this
->
hasMany
(
'Item'
,
array
(
'id'
=>
'item_id'
))
return
$this
->
hasMany
(
'Item'
,
array
(
'id'
=>
'item_id'
))
->
viaTable
(
'tbl_order_item'
,
array
(
'order_id'
=>
'id'
))
->
viaTable
(
'tbl_order_item'
,
array
(
'order_id'
=>
'id'
))
...
...
tests/unit/data/ar/OrderItem.php
View file @
c9b104f0
...
@@ -9,12 +9,12 @@ class OrderItem extends ActiveRecord
...
@@ -9,12 +9,12 @@ class OrderItem extends ActiveRecord
return
'tbl_order_item'
;
return
'tbl_order_item'
;
}
}
public
function
o
rder
()
public
function
getO
rder
()
{
{
return
$this
->
hasOne
(
'Order'
,
array
(
'id'
=>
'order_id'
));
return
$this
->
hasOne
(
'Order'
,
array
(
'id'
=>
'order_id'
));
}
}
public
function
i
tem
()
public
function
getI
tem
()
{
{
return
$this
->
hasOne
(
'Item'
,
array
(
'id'
=>
'item_id'
));
return
$this
->
hasOne
(
'Item'
,
array
(
'id'
=>
'item_id'
));
}
}
...
...
tests/unit/framework/db/ActiveRecordTest.php
View file @
c9b104f0
...
@@ -105,7 +105,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -105,7 +105,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$orders
=
$customer
->
orders
;
$orders
=
$customer
->
orders
;
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
count
(
$orders
));
$orders
=
$customer
->
o
rders
()
->
where
(
'id=3'
)
->
all
();
$orders
=
$customer
->
getO
rders
()
->
where
(
'id=3'
)
->
all
();
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
3
,
$orders
[
0
]
->
id
);
$this
->
assertEquals
(
3
,
$orders
[
0
]
->
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