Soy nuevo en yii2 y estoy buscando la forma de agregar un valor a la base de datos, desde un botton que tengo creado en un Gridview. O sea quisiera que al hacer click en el botton me insertara en un campo que tengo en la base de datos un valor. Saludos
3 respuestas
Aqui está el código de la vista en un Gridview. Lo he tomado de otro código. En realidad quisiera saber, como puedo al darle click al primer botón, me inserte un 1 en la base de datos, y un dos al darle click al segundo botón. Tengo en la base de datos un campo llamado status.
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel app\models\EventoSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Eventos';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="evento-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create Evento', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'id',
'user_id',
'type',
'days_taken',
'description',
[
//'header' => Yii::t('app', 'Status'),
'value' => function ($model) {
if ($model->isApproved) {
return '<div class="text-center">
<span class="text-success">' . Yii::t('app', 'Approved') . '</span>
</div>';
} else {
return Html::a(Yii::t('app', 'Approved'), ['approved', 'id' => $model->id], [
'class' => 'btn btn-xs btn-success btn-block',
'data-method' => 'post',
//'data-confirm' => Yii::t('app', 'Are you sure you want to approved this user?'),
]);
}
},
'format' => 'raw',
//'visible' => Yii::$app->getModule('user')->enableConfirmation,
],
[
//'header' => Yii::t('app', 'Status'),
'value' => function ($model) {
if ($model->isDenied) {
return '<div class="text-center">
<span class="text-success">' . Yii::t('app', 'Denied') . '</span>
</div>';
} else {
return Html::a(Yii::t('app', 'Denied'), ['denied', 'id' => $model->id], [
'class' => 'btn btn-xs btn-success btn-danger',
'data-method' => 'post',
//'data-confirm' => Yii::t('app', 'Are you sure you want to denied this user?'),
]);
}
},
'format' => 'raw',
//'visible' => Yii::$app->getModule('user')->enableConfirmation,
],
['class' => 'yii\grid\ActionColumn',],
],
]); ?>
</div>
Este es mi modelo...
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "evento".
*
* @property int $id
* @property int $user_id
* @property string $days_taken
* @property string $description
* @property string $type
* @property string $date_end
* @property string $date_init
* @property string $update_at
* @property string $created_at
* @property int $status
*/
class Evento extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'evento';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['user_id', 'days_taken', 'description', 'type'], 'required'],
[['user_id', 'status'], 'integer'],
[['date_end', 'date_init', 'update_at', 'created_at'], 'safe'],
[['days_taken', 'description', 'type'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'days_taken' => 'Days Taken',
'description' => 'Description',
'type' => 'Type',
'date_end' => 'Date End',
'date_init' => 'Date Init',
'update_at' => 'Update At',
'created_at' => 'Created At',
'status' => 'Status',
];
}
}
<?php
namespace app\controllers;
use Yii;
use app\models\Evento;
use app\models\EventoSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\data\ActiveDataProvider;
/**
* EventoController implements the CRUD actions for Evento model.
*/
class EventoController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Evento models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new EventoSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if(\Yii::$app->user->can('SeeAdmin')){
if ($searchModel->status == 'isApproved'){
$searchModel->status = 2;
}
elseif ($searchModel->status == 'isDenied') {
$searchModel->status = 3;
}
else {$searchModel->status = 1;}
return $this->render('index_admin', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}else{
$query = \app\models\User::find()->where(['id' => Yii::$app->user->identity->id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $this->render('index', ['dataProvider' => $dataProvider, 'query' => $query]);
}
}
/**
* Displays a single Evento model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Evento model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Evento();
// Serverseitige Überprüfung fehtl noch
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
if(\Yii::$app->user->can('SeeAdmin')){
$view = "_form_admin";
//$model->status="approved";
// denied
}else{
$model->user_id=\Yii::$app->user->identity->id;
//$model->status="requested";
$view = "_form";
}
return $this->render('create', [
'model' => $model,
'view' => $view,
]);
}
/**
* Updates an existing Evento model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Evento model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Evento model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Evento the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Evento::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}