我在Yii2中点击购物车按钮时如何获取数据

时间:2018-04-24 10:32:59

标签: yii2

我是Yii2的新手,点击按钮时从另一个表中获取数据时遇到问题,例如:与购物网类似,当我点击“订单”时,我会去订购页面,我不需要填写我的名字,东西和另一个,它将自动填写。 我有2张桌子

  1. kntn_menu_paket

    • menu_paket_id(PK)
    • nama_menu_paket
    • 价格
    • 甘巴尔
    • status_menu
  2. kntn_r_pesanan

    • pesanan_id(PK)
    • menu_paket_id(FK)
    • tgl_pengambilan
  3. kntn_menu_paket(model)

    class KntnMenuPaket extends \yii\db\ActiveRecord
    {
    
        /**
         * behaviour to add created_at and updatet_at field with current datetime (timestamp)
         * and created_by and updated_by field with current user id (blameable)
         */
        public function behaviors(){
            return [
                'timestamp' => [
                    'class' => TimestampBehavior::className(),
                ],
                'blameable' => [
                    'class' => BlameableBehavior::className(),
                ],
                'del' => [
                    'class' => DeleteBehavior::className(),
                ]
            ];
        }
    
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'kntn_menu_paket';
        }
    
        /**
         * @inheritdoc
         */
    
        public function rules()
        {
            return [
                [['harga', 'nama_menu_paket','deskripsi'], 'required'],
                [['harga', 'deleted'], 'integer'],
                [['created_at', 'updated_at', 'deleted_at'], 'safe'],
                [['nama_menu_paket', 'created_by', 'updated_by','deskripsi','status_menu','harga'], 'string', 'max' => 1000],
                [['gambar'], 'file','extensions' =>'jpeg, jpg, png, gif'],
                [['deleted_by'], 'string', 'max' => 32],
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'harga' => 'Harga',
                'menu_paket_id' => 'Menu Paket ID',
                'nama_menu_paket' => 'Nama Menu Paket',
                'gambar' => 'Gambar',
                'deskripsi' =>'Deskripsi',
                'status_menu'=>'Status Makanan',
                //..
            ];
        }
    
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getKntnRPesanans()
        {
            return $this->hasMany(KntnRPesanan::className(), ['menu_paket_id' => 'menu_paket_id']);
        }
    }
    

    kntn_r_pesanan(model)

    class KntnRPesanan extends \yii\db\ActiveRecord
    {
    
        /**
         * behaviour to add created_at and updatet_at field with current datetime (timestamp)
         * and created_by and updated_by field with current user id (blameable)
         */
        public function behaviors(){
            return [
                'timestamp' => [
                    'class' => TimestampBehavior::className(),
                ],
                'blameable' => [
                    'class' => BlameableBehavior::className(),
                ],
                'delete' => [
                    'class' => DeleteBehavior::className(),
                ]
            ];
        }
    
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'kntn_r_pesanan';
        }
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['tgl_pengambilan', 'jlh_pesanan', 'acara', 'alamat', 'menu_paket_id', 'status_pesanan'], 'required'],
                [['created_at', 'updated_at', 'deleted_at'], 'safe'],
                [['deleted', 'menu_paket_id'], 'integer'],
                [['tgl_pengambilan', 'jlh_pesanan', 'acara', 'created_by', 'updated_by'], 'string', 'max' => 45],
                [['alamat'], 'string', 'max' => 200],
                [['deleted_by'], 'string', 'max' => 32],
                [['status_pesanan'], 'string', 'max' => 100],
                [['menu_paket_id'], 'exist', 'skipOnError' => true, 'targetClass' => KntnMenuPaket::className(), 'targetAttribute' => ['menu_paket_id' => 'menu_paket_id']]
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'pesanan_id' => 'Pesanan ID',
                'tgl_pengambilan' => 'Tgl Pengambilan',
                'jlh_pesanan' => 'Jlh Pesanan',
          //..
            ];
        }
    
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getMenuPaket()
        {
            return $this->hasOne(KntnMenuPaket::className(), ['menu_paket_id' => 'menu_paket_id']);
        }
    
    }
    

    kntn_r_pesanan/index

    <div class="kntn-r-pesanan-indexuser">
    
        <h1><?= Html::encode($this->title) ?></h1>
        <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
    
    
        <p>
            <?= Html::a('<i class="glyphicon glyphicon-shopping-cart"> Pesananku</i>', ['kntn-r-pesanan/history'], ['class' => 'btn btn-default glyphicon glyphicon-chart'])?>
        </p>
    
    
    
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'columns' => [
                ['class' => 'yii\grid\SerialColumn'],
    
                //'harga',
    
    
                [
                    'attribute' => 'harga',
                    'filter' => '',
                    'value' => function($model){
                        return "Rp".number_format($model->harga,2,',','.');
                    },
                ],
    
                //'menu_paket_id',
                'nama_menu_paket',
    
                [
                    'attribute' => 'gambar',
                    'format' => ['image',['width'=>'100','height'=>'100']],
                ],
                //'created_at',
                'deskripsi',
                'status_menu',
                // 'updated_at',
                // 'created_by',
                // 'updated_by',
                // 'deleted',
                // 'deleted_at',
                // 'deleted_by',
    
                ['class' => 'common\components\ToolsColumn',
                    'template'=>'{view} {edit} {del} {cart}',
                    'urlCreator' => function ($action, $model, $key, $index){
                        if ($action === 'view') {
                            return Url::toRoute(['view', 'id' => $key]);
                        }
                        if ($action === 'edit') {
                            return Url::toRoute(['edit', 'id' => $key]);
                        }
                        if ($action === 'del') {
                            return Url::toRoute(['delete', 'id' => $key]);
                        }
                        if($action ==='cart'){
                            return Url::toRoute(['cart', 'id'=>$key]);
                        }
                    }
                ],
    
            ],
        ]); ?>
    

    当我点击图表按钮时,我将转到_form,nama_menu_paket将从此索引中检索

    kntn_r_pesanan(_form)

    <div class="kntn-r-pesanan-form">
    
        <?php $form = ActiveForm::begin(); ?>
    
    
        <?= $form->field($model, 'menu_paket_id')->textInput(['maxlength' => true]) ?>
    
        <!--<?= $form->field($model, 'tgl_pengambilan')->textInput(['maxlength' => true]) ?>-->
    
    
        <?= $form->field($model, 'jlh_pesanan')->textInput(['maxlength' => true]) ?>
    
        <?= $form->field($model, 'acara')->textInput(['maxlength' => true]) ?>
    
        <?= $form->field($model, 'alamat')->textInput(['maxlength' => true]) ?>
    
    
    
        <div class="form-group">
            <?= Html::submitButton($model->isNewRecord ? 'Tambah' : 'Edit', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
        </div>
    
        <?php ActiveForm::end(); ?>
    
    </div>
    

1 个答案:

答案 0 :(得分:0)

如果我明白你的意思,你只需要自动填写表格中的一些字段。

因此,您只需要进入控制器操作,该操作将呈现您的表单,并使用其他表/模型中的数据设置模型的字段。您只需在渲染视图之前设置模型字段即可。

$model->your_field = $otherModel->other_field;