Commit fb8e8182 by Qiang Xue

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

parent c28a005f
......@@ -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');
}
}
......
......@@ -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()
......
......@@ -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
......
......@@ -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)
......
......@@ -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]);
}
......
......@@ -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']);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment