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
fb8e8182
Commit
fb8e8182
authored
Oct 21, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dropped support for supporting default namespace for classes of related models.
parent
c28a005f
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
28 additions
and
47 deletions
+28
-47
ActiveRecord.md
docs/api/db/ActiveRecord.md
+7
-8
active-record.md
docs/guide/active-record.md
+10
-10
ActiveRecord.php
framework/yii/db/ActiveRecord.php
+4
-22
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
No files found.
docs/api/db/ActiveRecord.md
View file @
fb8e8182
...
...
@@ -175,7 +175,7 @@ class Customer extends \yii\db\ActiveRecord
{
public function getOrders()
{
return $this->hasMany(
'Order'
, ['customer_id' => 'id']);
return $this->hasMany(
Order::className()
, ['customer_id' => 'id']);
}
}
...
...
@@ -183,7 +183,7 @@ class Order extends \yii\db\ActiveRecord
{
public function getCustomer()
{
return $this->hasOne(
'Customer'
, ['id' => 'customer_id']);
return $this->hasOne(
Customer::className()
, ['id' => 'customer_id']);
}
}
~~~
...
...
@@ -194,8 +194,7 @@ a one-many relationship. For example, a customer has many orders. And the [[hasO
method declares a many-one or one-one relationship. For example, an order has one customer.
Both methods take two parameters:
-
`$class`
: the name of the class related models should use. If specified without
a namespace, the namespace will be taken from the declaring class.
-
`$class`
: the name of the class that the related models should use.
-
`$link`
: the association between columns from two tables. This should be given as an array.
The keys of the array are the names of the columns from the table associated with
`$class`
,
while the values of the array are the names of the columns from the declaring class.
...
...
@@ -223,7 +222,7 @@ class Customer extends \yii\db\ActiveRecord
{
public function getBigOrders($threshold = 100)
{
return $this->hasMany(
'Order'
, ['customer_id' => 'id'])
return $this->hasMany(
Order::className()
, ['customer_id' => 'id'])
->where('subtotal > :threshold', [':threshold' => $threshold])
->orderBy('id');
}
...
...
@@ -244,7 +243,7 @@ class Order extends \yii\db\ActiveRecord
{
public function getItems()
{
return $this->hasMany(
'Item'
, ['id' => 'item_id'])
return $this->hasMany(
Item::className()
, ['id' => 'item_id'])
->viaTable('tbl_order_item', ['order_id' => 'id']);
}
}
...
...
@@ -259,12 +258,12 @@ class Order extends \yii\db\ActiveRecord
{
public function getOrderItems()
{
return $this->hasMany(
'OrderItem'
, ['order_id' => 'id']);
return $this->hasMany(
OrderItem::className()
, ['order_id' => 'id']);
}
public function getItems()
{
return $this->hasMany(
'Item'
, ['id' => 'item_id'])
return $this->hasMany(
Item::className()
, ['id' => 'item_id'])
->via('orderItems');
}
}
...
...
docs/guide/active-record.md
View file @
fb8e8182
...
...
@@ -202,7 +202,7 @@ class Customer extends \yii\db\ActiveRecord
{
public
function
getOrders
()
{
return
$this
->
hasMany
(
'Order'
,
[
'customer_id'
=>
'id'
]);
return
$this
->
hasMany
(
Order
::
className
,
[
'customer_id'
=>
'id'
]);
}
}
...
...
@@ -210,7 +210,7 @@ class Order extends \yii\db\ActiveRecord
{
public
function
getCustomer
()
{
return
$this
->
hasOne
(
'Customer'
,
[
'id'
=>
'customer_id'
]);
return
$this
->
hasOne
(
Customer
::
className
()
,
[
'id'
=>
'customer_id'
]);
}
}
```
...
...
@@ -257,7 +257,7 @@ class Customer extends \yii\db\ActiveRecord
{
public
function
getBigOrders
(
$threshold
=
100
)
{
return
$this
->
hasMany
(
'Order'
,
[
'customer_id'
=>
'id'
])
return
$this
->
hasMany
(
Order
::
className
()
,
[
'customer_id'
=>
'id'
])
->
where
(
'subtotal > :threshold'
,
[
':threshold'
=>
$threshold
])
->
orderBy
(
'id'
);
}
...
...
@@ -291,7 +291,7 @@ class Order extends \yii\db\ActiveRecord
{
public
function
getItems
()
{
return
$this
->
hasMany
(
'Item'
,
[
'id'
=>
'item_id'
])
return
$this
->
hasMany
(
Item
::
className
()
,
[
'id'
=>
'item_id'
])
->
viaTable
(
'tbl_order_item'
,
[
'order_id'
=>
'id'
]);
}
}
...
...
@@ -306,12 +306,12 @@ class Order extends \yii\db\ActiveRecord
{
public
function
getOrderItems
()
{
return
$this
->
hasMany
(
'OrderItem'
,
[
'order_id'
=>
'id'
]);
return
$this
->
hasMany
(
OrderItem
::
className
()
,
[
'order_id'
=>
'id'
]);
}
public
function
getItems
()
{
return
$this
->
hasMany
(
'Item'
,
[
'id'
=>
'item_id'
])
return
$this
->
hasMany
(
Item
::
className
()
,
[
'id'
=>
'item_id'
])
->
via
(
'orderItems'
);
}
}
...
...
@@ -517,7 +517,7 @@ class Feature extends \yii\db\ActiveRecord
public
function
getProduct
()
{
return
$this
->
hasOne
(
'Product'
,
[
'product_id'
=>
'id'
]);
return
$this
->
hasOne
(
Product
::
className
()
,
[
'product_id'
=>
'id'
]);
}
}
...
...
@@ -527,7 +527,7 @@ class Product extends \yii\db\ActiveRecord
public
function
getFeatures
()
{
return
$this
->
hasMany
(
'Feature'
,
[
'id'
=>
'product_id'
]);
return
$this
->
hasMany
(
Feature
::
className
()
,
[
'id'
=>
'product_id'
]);
}
}
```
...
...
@@ -566,7 +566,7 @@ class Feature extends \yii\db\ActiveRecord
public
function
getProduct
()
{
return
$this
->
hasOne
(
'Product'
,
[
'product_id'
=>
'id'
]);
return
$this
->
hasOne
(
Product
::
className
()
,
[
'product_id'
=>
'id'
]);
}
public
function
scenarios
()
...
...
@@ -586,7 +586,7 @@ class Product extends \yii\db\ActiveRecord
public
function
getFeatures
()
{
return
$this
->
hasMany
(
'Feature'
,
[
'id'
=>
'product_id'
]);
return
$this
->
hasMany
(
Feature
::
className
()
,
[
'id'
=>
'product_id'
]);
}
public
function
scenarios
()
...
...
framework/yii/db/ActiveRecord.php
View file @
fb8e8182
...
...
@@ -456,7 +456,7 @@ class ActiveRecord extends Model
* ~~~
* public function getCountry()
* {
* return $this->hasOne(
'Country'
, ['id' => 'country_id']);
* return $this->hasOne(
Country::className()
, ['id' => 'country_id']);
* }
* ~~~
*
...
...
@@ -475,7 +475,7 @@ class ActiveRecord extends Model
public
function
hasOne
(
$class
,
$link
)
{
return
new
ActiveRelation
([
'modelClass'
=>
$
this
->
getNamespacedClass
(
$class
)
,
'modelClass'
=>
$
class
,
'primaryModel'
=>
$this
,
'link'
=>
$link
,
'multiple'
=>
false
,
...
...
@@ -496,7 +496,7 @@ class ActiveRecord extends Model
* ~~~
* public function getOrders()
* {
* return $this->hasMany(
'Order'
, ['customer_id' => 'id']);
* return $this->hasMany(
Order::className()
, ['customer_id' => 'id']);
* }
* ~~~
*
...
...
@@ -513,7 +513,7 @@ class ActiveRecord extends Model
public
function
hasMany
(
$class
,
$link
)
{
return
new
ActiveRelation
([
'modelClass'
=>
$
this
->
getNamespacedClass
(
$class
)
,
'modelClass'
=>
$
class
,
'primaryModel'
=>
$this
,
'link'
=>
$link
,
'multiple'
=>
true
,
...
...
@@ -1439,24 +1439,6 @@ class ActiveRecord extends Model
}
/**
* Changes the given class name into a namespaced one.
* If the given class name is already namespaced, no change will be made.
* Otherwise, the class name will be changed to use the same namespace as
* the current AR class.
* @param string $class the class name to be namespaced
* @return string the namespaced class name
*/
protected
static
function
getNamespacedClass
(
$class
)
{
if
(
strpos
(
$class
,
'\\'
)
===
false
)
{
$reflector
=
new
\ReflectionClass
(
static
::
className
());
return
$reflector
->
getNamespaceName
()
.
'\\'
.
$class
;
}
else
{
return
$class
;
}
}
/**
* @param array $link
* @param ActiveRecord $foreignModel
* @param ActiveRecord $primaryModel
...
...
tests/unit/data/ar/Customer.php
View file @
fb8e8182
...
...
@@ -24,7 +24,7 @@ class Customer extends ActiveRecord
public
function
getOrders
()
{
return
$this
->
hasMany
(
'Order'
,
[
'customer_id'
=>
'id'
])
->
orderBy
(
'id'
);
return
$this
->
hasMany
(
Order
::
className
()
,
[
'customer_id'
=>
'id'
])
->
orderBy
(
'id'
);
}
public
static
function
active
(
$query
)
...
...
tests/unit/data/ar/Order.php
View file @
fb8e8182
...
...
@@ -19,17 +19,17 @@ class Order extends ActiveRecord
public
function
getCustomer
()
{
return
$this
->
hasOne
(
'Customer'
,
[
'id'
=>
'customer_id'
]);
return
$this
->
hasOne
(
Customer
::
className
()
,
[
'id'
=>
'customer_id'
]);
}
public
function
getOrderItems
()
{
return
$this
->
hasMany
(
'OrderItem'
,
[
'order_id'
=>
'id'
]);
return
$this
->
hasMany
(
OrderItem
::
className
()
,
[
'order_id'
=>
'id'
]);
}
public
function
getItems
()
{
return
$this
->
hasMany
(
'Item'
,
[
'id'
=>
'item_id'
])
return
$this
->
hasMany
(
Item
::
className
()
,
[
'id'
=>
'item_id'
])
->
via
(
'orderItems'
,
function
(
$q
)
{
// additional query configuration
})
->
orderBy
(
'id'
);
...
...
@@ -37,7 +37,7 @@ class Order extends ActiveRecord
public
function
getBooks
()
{
return
$this
->
hasMany
(
'Item'
,
[
'id'
=>
'item_id'
])
return
$this
->
hasMany
(
Item
::
className
()
,
[
'id'
=>
'item_id'
])
->
viaTable
(
'tbl_order_item'
,
[
'order_id'
=>
'id'
])
->
where
([
'category_id'
=>
1
]);
}
...
...
tests/unit/data/ar/OrderItem.php
View file @
fb8e8182
...
...
@@ -19,11 +19,11 @@ class OrderItem extends ActiveRecord
public
function
getOrder
()
{
return
$this
->
hasOne
(
'Order'
,
[
'id'
=>
'order_id'
]);
return
$this
->
hasOne
(
Order
::
className
()
,
[
'id'
=>
'order_id'
]);
}
public
function
getItem
()
{
return
$this
->
hasOne
(
'Item'
,
[
'id'
=>
'item_id'
]);
return
$this
->
hasOne
(
Item
::
className
()
,
[
'id'
=>
'item_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