public function actionDonhang()
 {
     $criteria = new CDbCriteria();
     $iduser = Yii::app()->user->id;
     // $criteria->addCondition('id_user',$iduser);
     $criteria->compare('id_user', $iduser);
     $criteria->compare('status', 1);
     $list = Giohang::model()->findAll($criteria);
     $sum = 0;
     // luu vao don hang
     // tinh tong tien trong don hang
     foreach ($list as $data) {
         $sum = $sum + $data->cost * $data->number;
     }
     $donhang = new Donhang();
     $donhang->tongtien = $sum;
     $donhang->user_id = $iduser;
     $donhang->thoigian = time();
     $donhang->save();
     // luu vao chi tiet don hang
     foreach ($list as $data) {
         $model = new Ctdhang();
         $model->id_book = $data->id_book;
         $model->soluong = $data->number;
         $model->gia = $data->cost;
         $model->id_donhang = $donhang->id;
         $model->save();
         $data->delete();
     }
     $this->render('view', array('model' => $this->loadModel(3)));
 }
Exemplo n.º 2
0
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Giohang the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Giohang::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}