Browse Source

Dropped support for supporting default namespace for classes of related models.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
fb8e818204
  1. 15
      docs/api/db/ActiveRecord.md
  2. 20
      docs/guide/active-record.md
  3. 26
      framework/yii/db/ActiveRecord.php
  4. 2
      tests/unit/data/ar/Customer.php
  5. 8
      tests/unit/data/ar/Order.php
  6. 4
      tests/unit/data/ar/OrderItem.php

15
docs/api/db/ActiveRecord.md

@ -175,7 +175,7 @@ class Customer extends \yii\db\ActiveRecord
{ {
public function getOrders() 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() 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. method declares a many-one or one-one relationship. For example, an order has one customer.
Both methods take two parameters: Both methods take two parameters:
- `$class`: the name of the class related models should use. If specified without - `$class`: the name of the class that the related models should use.
a namespace, the namespace will be taken from the declaring class.
- `$link`: the association between columns from two tables. This should be given as an array. - `$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`, 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. 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) 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]) ->where('subtotal > :threshold', [':threshold' => $threshold])
->orderBy('id'); ->orderBy('id');
} }
@ -244,7 +243,7 @@ class Order extends \yii\db\ActiveRecord
{ {
public function getItems() 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']); ->viaTable('tbl_order_item', ['order_id' => 'id']);
} }
} }
@ -259,12 +258,12 @@ class Order extends \yii\db\ActiveRecord
{ {
public function getOrderItems() public function getOrderItems()
{ {
return $this->hasMany('OrderItem', ['order_id' => 'id']); return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
} }
public function getItems() public function getItems()
{ {
return $this->hasMany('Item', ['id' => 'item_id']) return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems'); ->via('orderItems');
} }
} }

20
docs/guide/active-record.md

@ -202,7 +202,7 @@ class Customer extends \yii\db\ActiveRecord
{ {
public function getOrders() 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() 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) 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]) ->where('subtotal > :threshold', [':threshold' => $threshold])
->orderBy('id'); ->orderBy('id');
} }
@ -291,7 +291,7 @@ class Order extends \yii\db\ActiveRecord
{ {
public function getItems() 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']); ->viaTable('tbl_order_item', ['order_id' => 'id']);
} }
} }
@ -306,12 +306,12 @@ class Order extends \yii\db\ActiveRecord
{ {
public function getOrderItems() public function getOrderItems()
{ {
return $this->hasMany('OrderItem', ['order_id' => 'id']); return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
} }
public function getItems() public function getItems()
{ {
return $this->hasMany('Item', ['id' => 'item_id']) return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems'); ->via('orderItems');
} }
} }
@ -517,7 +517,7 @@ class Feature extends \yii\db\ActiveRecord
public function getProduct() 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() 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() public function getProduct()
{ {
return $this->hasOne('Product', ['product_id' => 'id']); return $this->hasOne(Product::className(), ['product_id' => 'id']);
} }
public function scenarios() public function scenarios()
@ -586,7 +586,7 @@ class Product extends \yii\db\ActiveRecord
public function getFeatures() public function getFeatures()
{ {
return $this->hasMany('Feature', ['id' => 'product_id']); return $this->hasMany(Feature::className(), ['id' => 'product_id']);
} }
public function scenarios() public function scenarios()

26
framework/yii/db/ActiveRecord.php

@ -456,7 +456,7 @@ class ActiveRecord extends Model
* ~~~ * ~~~
* public function getCountry() * 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) public function hasOne($class, $link)
{ {
return new ActiveRelation([ return new ActiveRelation([
'modelClass' => $this->getNamespacedClass($class), 'modelClass' => $class,
'primaryModel' => $this, 'primaryModel' => $this,
'link' => $link, 'link' => $link,
'multiple' => false, 'multiple' => false,
@ -496,7 +496,7 @@ class ActiveRecord extends Model
* ~~~ * ~~~
* public function getOrders() * 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) public function hasMany($class, $link)
{ {
return new ActiveRelation([ return new ActiveRelation([
'modelClass' => $this->getNamespacedClass($class), 'modelClass' => $class,
'primaryModel' => $this, 'primaryModel' => $this,
'link' => $link, 'link' => $link,
'multiple' => true, '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 array $link
* @param ActiveRecord $foreignModel * @param ActiveRecord $foreignModel
* @param ActiveRecord $primaryModel * @param ActiveRecord $primaryModel

2
tests/unit/data/ar/Customer.php

@ -24,7 +24,7 @@ class Customer extends ActiveRecord
public function getOrders() 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) public static function active($query)

8
tests/unit/data/ar/Order.php

@ -19,17 +19,17 @@ class Order extends ActiveRecord
public function getCustomer() public function getCustomer()
{ {
return $this->hasOne('Customer', ['id' => 'customer_id']); return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
} }
public function getOrderItems() public function getOrderItems()
{ {
return $this->hasMany('OrderItem', ['order_id' => 'id']); return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
} }
public function getItems() public function getItems()
{ {
return $this->hasMany('Item', ['id' => 'item_id']) return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems', function ($q) { ->via('orderItems', function ($q) {
// additional query configuration // additional query configuration
})->orderBy('id'); })->orderBy('id');
@ -37,7 +37,7 @@ class Order extends ActiveRecord
public function getBooks() 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']) ->viaTable('tbl_order_item', ['order_id' => 'id'])
->where(['category_id' => 1]); ->where(['category_id' => 1]);
} }

4
tests/unit/data/ar/OrderItem.php

@ -19,11 +19,11 @@ class OrderItem extends ActiveRecord
public function getOrder() public function getOrder()
{ {
return $this->hasOne('Order', ['id' => 'order_id']); return $this->hasOne(Order::className(), ['id' => 'order_id']);
} }
public function getItem() public function getItem()
{ {
return $this->hasOne('Item', ['id' => 'item_id']); return $this->hasOne(Item::className(), ['id' => 'item_id']);
} }
} }

Loading…
Cancel
Save