我希望通过在alumnys2中使用table alumniintegrasi之间的关系来显示数据关系,但是当我用gridview创建它时,出现了错误。
它说'
获取未知属性:app \ models \ AlumnyS2 :: alumniintegrasi。
这是代码
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use app\models\AlumniIntegrasi;
/* @var $this yii\web\View */
/* @var $searchModel app\models\AlumnyS2Search */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Alumny S2s');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="alumny-s2-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a(Yii::t('app', 'Create Alumny S2'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'NRP',
//'NamaMahasiswa',
[
'attribute' => 'NamaMahasiswa',
'value' => function($data) {
return $data->alumniintegrasi->NamaMahasiswa;
},
/*'filter' => Html::activeDropDownList($searchModel, 'orang_ID', ArrayHelper::map(Orang::find()->asArray()->all(), 'ID', 'Nama'),['class'=>'form-control','prompt' => 'Select Category']),
*/
],
'ProgramStudi',
'Tanggal_Masuk',
'Tanggal_Lulus',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
这是模型
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "alumnys2".
*
* @property string $NRP
* @property string $NamaMahasiswa
* @property string $ProgramStudi
* @property string $Tanggal_Masuk
* @property string $Tanggal_Lulus
*
* @property AlumniIntegrasi $nRP
*/
class AlumnyS2 extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'alumnys2';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['NRP', 'NamaMahasiswa'], 'required'],
[['NRP'], 'string', 'max' => 15],
[['NamaMahasiswa'], 'string', 'max' => 50],
[['ProgramStudi'], 'string', 'max' => 5],
[['Tanggal_Masuk', 'Tanggal_Lulus'], 'string', 'max' => 30],
[['NRP'], 'exist', 'skipOnError' => true, 'targetClass' => AlumniIntegrasi::className(), 'targetAttribute' => ['NRP' => 'NRP']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'NRP' => Yii::t('app', 'Nrp'),
'NamaMahasiswa' => Yii::t('app', 'Nama Mahasiswa'),
'ProgramStudi' => Yii::t('app', 'Program Studi'),
'Tanggal_Masuk' => Yii::t('app', 'Tanggal Masuk'),
'Tanggal_Lulus' => Yii::t('app', 'Tanggal Lulus'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getNRP()
{
return $this->hasOne(AlumniIntegrasi::className(), ['NRP' => 'NRP']);
}
}
我想在表alumniintegrasi中展示NamaMahasiswa。我该如何解决?
答案 0 :(得分:0)
您应该为字段Can not locate SOAP.pm in @INC ( May you need to install the SOAP module) ( @INC contains: /Library/Perl/5.18/darwin-th. BEGIN failed - compilation aborted at clientWSperl.pl line 2
在您的模型中
更改模型getter的名称(名称
的aviding case convention问题)NamaMahasiswa
并添加getter只需使用属性名称(基于getter)
/**
* @return \yii\db\ActiveQuery
*/
public function getNrp()
{
return $this->hasOne(AlumniIntegrasi::className(), ['NRP' => 'NRP']);
}
视图中的
/* Getter for NamaMahasiswa */
public function getNamaMahasiswa() {
return $this->Nrp->NamaMahasiswa;
}
NB使用UpperCamleCase(Pascal Notation)作为数据库名称不是最佳选择..对于数据库更好用下划线注释..