2017-06-29 3 views
2

Entschuldigung im Anfänger für Framework Ich habe viele Tutorials von Doingiteasy Youtube Kanal gesehen und steckte in diesem Ich möchte ein Beispiel, um die ID_status zu 'Infostatus' zu ändern. Ich habe in den Videos gehört, um ID zu ändern, muss ich Beziehung verwenden. Wo finde ich das?yii2 - get name statt ID aus der Datenbank

das ist meine Ansicht Index

<?php 

use yii\helpers\Html; 
use yii\grid\GridView; 

/* @var $this yii\web\View */ 
/* @var $searchModel app\models\ReservasiSearch */ 
/* @var $dataProvider yii\data\ActiveDataProvider */ 

$this->title = 'Reservasis'; 
$this->params['breadcrumbs'][] = $this->title; 
?> 
<div class="reservasi-index"> 

    <h1><?= Html::encode($this->title) ?></h1> 
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?> 

    <p> 
     <?= Html::a('Create Reservasi', ['create'], ['class' => 'btn btn-success']) ?> 
    </p> 
    <?= GridView::widget([ 
     'dataProvider' => $dataProvider, 
     'filterModel' => $searchModel, 
     'columns' => [ 
      ['class' => 'yii\grid\SerialColumn'], 

      'id_reservasi', 
      'id_paket', 
      **'id_status',** 
      'id_jadwal', 
      'id', 
      // 'tanggalreservasi', 

      ['class' => 'yii\grid\ActionColumn'], 
     ], 
    ]); ?> 
</div> 

das mein Modell ist aus reservasi

<?php 

namespace app\models; 

use Yii; 

/** 
* This is the model class for table "reservasi". 
* 
* @property string $id_reservasi 
* @property string $id_paket 
* @property integer $id_status 
* @property string $id_jadwal 
* @property string $id 
* @property string $tanggalreservasi 
* 
* @property User $id0 
* @property Jadwal $idJadwal 
* @property Paket $idPaket 
* @property Status $idStatus 
*/ 
class Reservasi extends \yii\db\ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'reservasi'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['id_reservasi', 'tanggalreservasi'], 'required'], 
      [['id_status'], 'integer'], 
      [['tanggalreservasi'], 'safe'], 
      [['id_reservasi'], 'string', 'max' => 15], 
      [['id_paket'], 'string', 'max' => 5], 
      [['id_jadwal'], 'string', 'max' => 4], 
      [['id'], 'string', 'max' => 6], 
      [['id'], 'exist', 'skipOnError' => true, 'targetClass' => Users::className(), 'targetAttribute' => ['id' => 'id']], 
      [['id_jadwal'], 'exist', 'skipOnError' => true, 'targetClass' => Jadwal::className(), 'targetAttribute' => ['id_jadwal' => 'id_jadwal']], 
      [['id_paket'], 'exist', 'skipOnError' => true, 'targetClass' => Paket::className(), 'targetAttribute' => ['id_paket' => 'id_paket']], 
      [['id_status'], 'exist', 'skipOnError' => true, 'targetClass' => Status::className(), 'targetAttribute' => ['id_status' => 'id_status']], 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'id_reservasi' => 'Id Reservasi', 
      'id_paket' => 'Id Paket', 
      'id_status' => 'Id Status', 
      'id_jadwal' => 'Id Jadwal', 
      'id' => 'ID', 
      'tanggalreservasi' => 'Tanggalreservasi', 
     ]; 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getId0() 
    { 
     return $this->hasOne(Users::className(), ['id' => 'id']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getIdJadwal() 
    { 
     return $this->hasOne(Jadwal::className(), ['id_jadwal' => 'id_jadwal']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getIdPaket() 
    { 
     return $this->hasOne(Paket::className(), ['id_paket' => 'id_paket']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getIdStatus() 
    { 
     return $this->hasOne(Status::className(), ['id_status' => 'id_status']); 
    } 
} 

und das ist von Modell-Status

Antwort

1

Sie eine Beziehung zu Ihrem Reservasi könnte hinzufügen Modell für den Infostatus

Angenommen, Sie ein Modell namens Status haben, die Felder id und infostatus enthalten

/* ActiveRelation */ 
    public function getInfostatus() 
    { 
     return $this->hasOne(Status::className(), ['id' => 'id_status']); 
    } 

dann könnte man auf

verweisen
$model->infostatus; // for get the value 
+0

meinen Sie [ 'id_status' => 'id_status']); ? Ursache-ID wird in Benutzertabelle verwendet. und wo kann ich dieses $ model-> infostatus setzen? ? Was ist mit der Aussicht? Ich habe gehört, dass ich so ändern muss 'id_stats (Beziehung oder etwas, das ich rightnow verwirrt) .infostatus' –

+0

Überprüfen Sie für Ihr Statusmodell ... die ID in Beziehung ist die ID der Statusmodellklasse .. .. und Sie als Antwort hast du die Daten in deinem $ model-> infostatus .. – scaisEdge

+0

ahh ich bekomme es, danke Alter –

Verwandte Themen