Hola, me pueden ayudar por favor.
Tengo una tabla dende debo ingresar solo 2 datos, mas los datos created y updated, los que tengo seteados en MyActiveRecord.
los campos de la tabla estados son:
id INT NO NULL AUTO_INCREMENT PRIMARY KEY,
estado VARCHAR(30) NOT NULL,
grupo INT(2) NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL
created_by INT,
updated_by INT.
El modelo Estados.php.
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
/**
-
This is the model class for table “estados”.
-
@property int $id
-
@property string $estado
-
@property int $grupo
-
@property string $created_at
-
@property string $updated_at
-
@property int $created_by
-
@property int $updated_by
/
class Estados extends MyActiveRecord
{
/*- {@inheritdoc}
*/
public static function tableName()
{
return ‘estados’;
}
/**
- {@inheritdoc}
*/
public function rules()
{
return [
[[‘estado’, ‘grupo’, ‘created_at’, ‘updated_at’], ‘required’],
[[‘grupo’, ‘created_by’, ‘updated_by’], ‘integer’],
[[‘grupo’, ‘created_at’, ‘updated_at’], ‘safe’],
[[‘estado’], ‘string’, ‘max’ => 30],
];
}
/**
- {@inheritdoc}
*/
public function attributeLabels()
{
return [
‘id’ => ‘ID’,
‘estado’ => ‘Estado’,
‘grupo’ => ‘Grupo’,
‘created_at’ => ‘Created At’,
‘updated_at’ => ‘Updated At’,
‘created_by’ => ‘Created By’,
‘updated_by’ => ‘Updated By’,
];
}
}
- {@inheritdoc}
El Controlador EstadosController.php
<?php
namespace app\modules\admin\controllers;
use Yii;
use app\models\Estados;
use app\models\EstadosSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
-
EstadosController implements the CRUD actions for Estados model.
/
class EstadosController extends MyController
{
/*-
Lists all Estados models.
-
@return mixed
*/
public function actionIndex()
{
$searchModel = new EstadosSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);return $this->render(‘index’, [
‘searchModel’ => $searchModel,
‘dataProvider’ => $dataProvider,
]);
}
/**
- Displays a single Estados 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 Estados model.
-
If creation is successful, the browser will be redirected to the ‘view’ page.
-
@return mixed
*/
public function actionCreate()
{
$model = new Estados();if ($model->load(Yii::$app->request->post())) {
if ( $model->save() ) { $this->msgFlashSuccessCreate($model); } else { $this->msgFlashErrorCreate($model); } return $this->redirect(['index']);
} else {
return $this->render(‘create’, [
‘model’ => $model,
]);
}
}
/**
-
Updates an existing Estados 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())) {if ( $model->save() ) { $this->msgFlashSuccessUpdate($model); } else { $this->msgFlashErrorUpdate($model); } return $this->redirect(['index']);
} else {
return $this->render(‘update’, [
‘model’ => $model,
]);
}
}
/**
-
Deletes an existing Estados 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 Estados model based on its primary key value.
-
If the model is not found, a 404 HTTP exception will be thrown.
-
@param integer $id
-
@return Estados the loaded model
-
@throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Estados::findOne($id)) !== null) {
return $model;
}throw new NotFoundHttpException(‘The requested page does not exist.’);
}
}
-
MyActiveRecord.php
<?php
namespace app\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\behaviors\BlameableBehavior;
/**
- Modelo con la clase MiActiveRecor
- Metodo Behaviors par toda la aplicación
*/
class MyActiveRecord extends ActiveRecord
{
public function Behaviors()
{
return [
‘timestamp’ => [
‘class’ => TimestampBehavior::className(),
‘attributes’ => [
ActiveRecord::EVENT_BEFORE_INSERT => [‘created_at’, ‘updated_at’],
ActiveRecord::EVENT_BEFORE_UPDATE => [‘updated_at’]
],
‘value’ => new Expression(‘NOW()’),
],
‘blameable’ => [
‘class’ => BlameableBehavior::className(),
‘createdByAttribute’ => ‘created_by’,
‘updatedByAttribute’ => ‘updated_by’,
],
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCreatedBy()
{
return $this->hasOne(Users::className(), ['id' => 'created_by']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getUpdatedBy()
{
return $this->hasOne(Users::className(), ['id' => 'updated_by']);
}
public function getNombres()
{
return $this->nombre . " " . $this->apellidos;
}
}
MyController.php
<?php
namespace app\modules\admin\controllers;
use Yii;
// use app\models\Facturacion;
// use app\models\FacturacionSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
/**
-
FacturacionController implements the CRUD actions for Facturacion model.
/
class MyController extends Controller
{
public $layout = ‘template’;
/*- {@inheritdoc}
*/
public function behaviors()
{
return [
‘verbs’ => [
‘class’ => VerbFilter::className(),
‘actions’ => [
‘delete’ => [‘POST’],
],
],
‘access’ => [
‘class’ => AccessControl::className(),
‘rules’ => [
[
‘allow’ => true,
‘roles’ => [’@’],
],
],
],
];
}
public function msgFlashErrorCreate($model)
{
$model = new $model();Yii::$app->session->setFlash("error", "Error!!! El registro no fue guardado");
}
public function msgFlashSuccessCreate($model)
{
$model = new $model();Yii::$app->session->setFlash("success", "Registro guardado exitosamente");
}
public function msgFlashErrorUpdate($model)
{
$model = new $model();Yii::$app->session->setFlash("error", "Error!!! El registro no fue modificado");
}
public function msgFlashSuccessUpdate($model)
{
$model = new $model();Yii::$app->session->setFlash("success", "Registro modificado exitosamente");
}
} - {@inheritdoc}
me envia el mensaje de la function msgFlashErrorUpdate($model)
Yii::$app->session->setFlash(“error”, “Error!!! El registro no fue modificado”);
Por favor, no puedo entender el porque, reviso el modelo las validaciones y no puedo entender que pasa.