public static function getCategoryById($id) { if ($id != NULL) { $name = Categoria::findOne($id); return $name->nombre; } return "No Asignado"; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Categoria::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, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]); $query->andFilterWhere(['like', 'categoria', $this->categoria])->andFilterWhere(['like', 'seo_slug', $this->seo_slug])->andFilterWhere(['like', 'imagen', $this->imagen]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Categoria::find(); $query->joinWith(['user']); $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, 'create_user' => $this->create_user, 'create_time' => $this->create_time, 'update_user' => $this->update_user, 'update_time' => $this->update_time]); $query->andFilterWhere(['like', 'nombre', $this->nombre])->andFilterWhere(['like', 'descripcion', $this->descripcion])->andFilterWhere(['like', 'user.username', $this->user]); return $dataProvider; }
use yii\widgets\DetailView; use backend\models\User; use kartik\datecontrol\DateControl; use common\models\Categoria; /* @var $this yii\web\View */ /* @var $model common\models\Menu */ $this->title = $model->nombre; $this->params['breadcrumbs'][] = ['label' => 'Menús', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; ?> <div class="menu-view"> <h1><?php echo Html::encode($this->title); ?> </h1> <p> <?php echo Html::a('Actualizar', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']); ?> <?php echo Html::a('Eliminar', ['delete', 'id' => $model->id], ['class' => 'btn btn-danger', 'data' => ['confirm' => 'Are you sure you want to delete this item?', 'method' => 'post']]); ?> </p> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['id', ['attribute' => 'id_categoria', 'value' => Categoria::getCategoryById($model->id_categoria)], 'nombre', 'descripcion', 'precio', ['attribute' => 'create_user', 'value' => User::getUserById($model->create_user)], ['attribute' => 'create_time', 'value' => Yii::$app->formatter->asDate($model->create_time, "full")], ['attribute' => 'update_user', 'value' => User::getUserById($model->update_user)], ['attribute' => 'update_time', 'format' => ['date', 'php:l, d \\d\\e F \\d\\e\\l Y']]]]); ?> </div>
<div class="noticia-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'titulo')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'detalle')->widget(CKEditor::className(), ['options' => ['rows' => 6], 'preset' => 'advance']); ?> <?php echo $form->field($model, 'categoria_id')->widget(Select2::classname(), ['data' => \yii\helpers\ArrayHelper::map(\common\models\Categoria::find()->all(), 'id', 'categoria'), 'language' => 'es', 'options' => ['placeholder' => 'Seleccione ...'], 'pluginOptions' => ['allowClear' => true]]); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getCategoria() { return $this->hasOne(Categoria::className(), ['id' => 'categoria_id']); }
$this->title = 'Noticias'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="noticia-index"> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title"> <?php echo Html::a('Crear Noticia', ['create'], ['class' => 'btn btn-info']); ?> </h3> </div> <div class="panel-body"> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'titulo', ['attribute' => 'categoria_id', 'value' => 'categoria.categoria', 'format' => 'raw', 'filter' => Select2::widget(['model' => $searchModel, 'attribute' => 'categoria_id', 'data' => \yii\helpers\ArrayHelper::map(\common\models\Categoria::find()->all(), 'id', 'categoria'), 'options' => ['placeholder' => 'Seleccione...'], 'pluginOptions' => ['allowClear' => true]])], ['attribute' => 'created_by', 'value' => 'createdBy.name'], ['class' => 'yii\\grid\\ActionColumn']]]); ?> </div> </div> </div>
public function actionNoticia($slug) { $categorias = Categoria::find()->all(); $noticia = Noticia::find()->where("seo_slug = :slug", [":slug" => $slug])->orderBy('id')->one(); /* CONSULTA DE LOS COMENTARIOS DESDE EL CONTROLADOR $idNoticia=$noticia->id; $comentarios= Comentario::find() ->where("noticia_id = :idNoticia", [":idNoticia" => $idNoticia]) ->all(); */ $comentario = new Comentario(); if ($comentario->load(Yii::$app->request->post())) { $comentario->estado = '0'; $comentario->noticia_id = $noticia->id; $comentario->fecha = new Expression("NOW()"); if ($comentario->save()) { Yii::$app->session->setFlash('success', 'Gracias por su comentario'); } else { Yii::$app->session->setFlash('error', 'Su comentario no pudo ser registrado'); } return $this->redirect(["/noticia/{$slug}"]); } return $this->render('noticia', ['comentario' => $comentario, 'categorias' => $categorias, 'noticia' => $noticia]); }
use common\models\Categoria; /* @var $this yii\web\View */ /* @var $model common\models\Menu */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="menu-form"> <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); //if(isset($imagenes)) // $model->imagenes = $imagenes; ?> <?php echo $form->field($model, 'id_categoria')->dropDownList(ArrayHelper::map(Categoria::find()->all(), 'id', 'nombre'), ['prompt' => 'Seleccione categoría']); ?> <?php echo $form->field($model, 'nombre')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'descripcion')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'precio')->textInput(['maxlength' => true]); if (isset($imagenes)) { ?> <?php
/** * Finds the Categoria model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Categoria the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Categoria::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }