yii2 - 从数据库

时间:2017-06-29 18:06:43

标签: yii2 frameworks

对不起我是框架的新手 我已经看过很多来自Doingiteasy youtube频道的教程并且陷入了困境 我想要一个例子来改变id_status成为'infostatus'。 我在视频中听到,要将id改为name,我必须使用关系。哪里可以找到?

这是我的观点索引

<?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>

这是我来自储备的模型

<?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']);
    }
}

这是来自模型状态

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "status".
 *
 * @property integer $id_status
 * @property string $infostatus
 *
 * @property Reservasi[] $reservasis
 */
class Status extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'status';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_status'], 'required'],
            [['id_status'], 'integer'],
            [['infostatus'], 'string', 'max' => 20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_status' => 'Id Status',
            'infostatus' => 'Infostatus',
        ];
    }

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

1 个答案:

答案 0 :(得分:1)

您可以为您的Reservasi模型添加关系以获取信息量

假设您有一个名为Status的模型,其中包含字段id和infostatus

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

然后你可以参考

 $model->infostatus; // for get the value