mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-23 20:44:06 +08:00
add relationClassName to AR to allow different relation classes
... for different dbms
This commit is contained in:
@ -92,6 +92,10 @@ class ActiveRecord extends Model
|
|||||||
*/
|
*/
|
||||||
const OP_ALL = 0x07;
|
const OP_ALL = 0x07;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string the name of the class to use for relations.
|
||||||
|
*/
|
||||||
|
protected $relationClassName = 'yii\\db\\ActiveRelation';
|
||||||
/**
|
/**
|
||||||
* @var array attribute values indexed by attribute names
|
* @var array attribute values indexed by attribute names
|
||||||
*/
|
*/
|
||||||
@ -384,7 +388,7 @@ class ActiveRecord extends Model
|
|||||||
return $this->_related[$t];
|
return $this->_related[$t];
|
||||||
}
|
}
|
||||||
$value = parent::__get($name);
|
$value = parent::__get($name);
|
||||||
if ($value instanceof ActiveRelation) {
|
if ($value instanceof $this->relationClassName) {
|
||||||
return $this->_related[$t] = $value->multiple ? $value->all() : $value->one();
|
return $this->_related[$t] = $value->multiple ? $value->all() : $value->one();
|
||||||
} else {
|
} else {
|
||||||
return $value;
|
return $value;
|
||||||
@ -474,7 +478,7 @@ class ActiveRecord extends Model
|
|||||||
*/
|
*/
|
||||||
public function hasOne($class, $link)
|
public function hasOne($class, $link)
|
||||||
{
|
{
|
||||||
return new ActiveRelation([
|
return new $this->relationClassName([
|
||||||
'modelClass' => $class,
|
'modelClass' => $class,
|
||||||
'primaryModel' => $this,
|
'primaryModel' => $this,
|
||||||
'link' => $link,
|
'link' => $link,
|
||||||
@ -512,7 +516,7 @@ class ActiveRecord extends Model
|
|||||||
*/
|
*/
|
||||||
public function hasMany($class, $link)
|
public function hasMany($class, $link)
|
||||||
{
|
{
|
||||||
return new ActiveRelation([
|
return new $this->relationClassName([
|
||||||
'modelClass' => $class,
|
'modelClass' => $class,
|
||||||
'primaryModel' => $this,
|
'primaryModel' => $this,
|
||||||
'link' => $link,
|
'link' => $link,
|
||||||
@ -1264,7 +1268,7 @@ class ActiveRecord extends Model
|
|||||||
$getter = 'get' . $name;
|
$getter = 'get' . $name;
|
||||||
try {
|
try {
|
||||||
$relation = $this->$getter();
|
$relation = $this->$getter();
|
||||||
if ($relation instanceof ActiveRelation) {
|
if ($relation instanceof $this->relationClassName) {
|
||||||
return $relation;
|
return $relation;
|
||||||
}
|
}
|
||||||
} catch (UnknownMethodException $e) {
|
} catch (UnknownMethodException $e) {
|
||||||
|
Reference in New Issue
Block a user