diff --git a/docs/api/db/ActiveRecord.md b/docs/api/db/ActiveRecord.md index 0120998..70f171b 100644 --- a/docs/api/db/ActiveRecord.md +++ b/docs/api/db/ActiveRecord.md @@ -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'); } } diff --git a/docs/guide/active-record.md b/docs/guide/active-record.md index 3de4a01..e0d16c9 100644 --- a/docs/guide/active-record.md +++ b/docs/guide/active-record.md @@ -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() diff --git a/framework/yii/db/ActiveRecord.php b/framework/yii/db/ActiveRecord.php index 3f158cf..806a6c1 100644 --- a/framework/yii/db/ActiveRecord.php +++ b/framework/yii/db/ActiveRecord.php @@ -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 diff --git a/tests/unit/data/ar/Customer.php b/tests/unit/data/ar/Customer.php index 441b37c..60a68b5 100644 --- a/tests/unit/data/ar/Customer.php +++ b/tests/unit/data/ar/Customer.php @@ -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) diff --git a/tests/unit/data/ar/Order.php b/tests/unit/data/ar/Order.php index b6392ed..6d5e926 100644 --- a/tests/unit/data/ar/Order.php +++ b/tests/unit/data/ar/Order.php @@ -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]); } diff --git a/tests/unit/data/ar/OrderItem.php b/tests/unit/data/ar/OrderItem.php index ad931a8..b340a46 100644 --- a/tests/unit/data/ar/OrderItem.php +++ b/tests/unit/data/ar/OrderItem.php @@ -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']); } }