/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = TransaksiItem::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'transaksi_id' => $this->transaksi_id, 'produk_id' => $this->produk_id, 'jumlah' => $this->jumlah]); return $dataProvider; }
public function actionUpdateD($id) { $modelTransaksi = $this->findModel($id); $modelsTransaksiItem = $modelTransaksi->addresses; if ($modelTransaksi->load(Yii::$app->request->post())) { $oldIDs = ArrayHelper::map($modelsTransaksiItem, 'id', 'id'); $modelsTransaksiItem = Model::createMultiple(TransaksiItem::classname(), $modelsTransaksiItem); Model::loadMultiple($modelsTransaksiItem, Yii::$app->request->post()); $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsTransaksiItem, 'id', 'id'))); // ajax validation if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ArrayHelper::merge(ActiveForm::validateMultiple($modelsTransaksiItem), ActiveForm::validate($modelTransaksi)); } // validate all models $valid = $modelTransaksi->validate(); $valid = Model::validateMultiple($modelsTransaksiItem) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $modelTransaksi->save(false)) { if (!empty($deletedIDs)) { TransaksiItem::deleteAll(['id' => $deletedIDs]); } foreach ($modelsTransaksiItem as $modelAddress) { $modelAddress->customer_id = $modelTransaksi->id; if (!($flag = $modelAddress->save(false))) { $transaction->rollBack(); break; } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $modelTransaksi->id]); } } catch (Exception $e) { $transaction->rollBack(); } } } return $this->render('_formD', ['modelTransaksi' => $modelTransaksi, 'modelsTransaksiItem' => empty($modelsTransaksiItem) ? [new Address()] : $modelsTransaksiItem]); }
/** * Finds the TransaksiItem model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return TransaksiItem the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = TransaksiItem::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getTransaksiItems() { return $this->hasMany(TransaksiItem::className(), ['produk_id' => 'id']); }