Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取Yii2中模型实例的URL_Url_Yii2_Polymorphic Associations - Fatal编程技术网

如何获取Yii2中模型实例的URL

如何获取Yii2中模型实例的URL,url,yii2,polymorphic-associations,Url,Yii2,Polymorphic Associations,我有一个多态关联: public function getImageable() { $classname = 'app\\models\\' . $this->imageable_type; return $this->hasOne( $classname, ['id' => 'imageable_id'] ); } 它可以返回不同模型的实例。我需要在索引视图中呈现一个指向imageable模型实例的链接。我该怎么做?我需要一些东西,为我提供imageabl

我有一个多态关联:

public function getImageable()
{
    $classname = 'app\\models\\' . $this->imageable_type;
    return $this->hasOne( $classname, ['id' => 'imageable_id'] );
}
它可以返回不同模型的实例。我需要在索引视图中呈现一个指向
imageable
模型实例的链接。我该怎么做?我需要一些东西,为我提供
imageable\u type
控制器的
视图
操作的URL,该控制器不是当前控制器

Url:to( $controllerId . '/view', 'id' => $this->imageable_id )

但我的问题是,如何从
模型
中确定
控制器ID

$classname是一个字符串

建造一个

function getImageableName() {
   return  'app\\models\\' . $this->imageable_type;
}
返回您需要的名称

 Url::to($model->getImageableName())

你想完成什么还不清楚。你创建了控制器动作的链接,而不是模型。编辑了我的问题以便更清楚。