我想在模型中添加一个字段,因此它显示在index / update / create。
中它也应该在update / create中可编辑。
在sendin / save from update / create之后,我希望使用该字段的内容,计算一些内容,然后将其与所有其他字段(它们在DB中具有相应的字段)一起写入现有DB字段中的数据库。
我可以在模型中添加一个新字段(frontend / model / poi.php
public $tempVal = NULL;
和同一档案中的规则
[['tempVal'], 'string', 'max' => 64],
因此,所有行在此字段中的内容都相同。 - >那不是我想要的。
该字段在每行中应具有不同的内容(比如随机数)。
我该如何解决这个问题?
答案 0 :(得分:0)
这是模型和控制器(不工作)
/frontend/models/PoiCore.php
class PoiCore extends \yii\db\ActiveRecord
{
public $tempVal = NULL;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'poi_core';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['owner'], 'required'],
[['owner'], 'integer'],
[['poi_name'], 'string', 'max' => 64],
[['tempVal'], 'string', 'max' => 64],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'owner' => Yii::t('app', 'Owner'),
'poi_name' => Yii::t('app', 'Poi Name'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getPoiDetail()
{
$data= $this->hasOne(PoiDetail::className(), ['poi_id' => 'id']);
return $data;
}
/**
* @inheritdoc
* @return PoiCoreQuery the active query used by this AR class.
*/
public static function find()
{
$data= new PoiCoreQuery(get_called_class());
return $data;
}
}
和控制器
<?php
namespace frontend\controllers;
use Yii;
use frontend\models\PoiCore;
use frontend\models\PoiSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* PoiController implements the CRUD actions for PoiCore model.
*/
class PoiController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all PoiCore models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new PoiSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single PoiCore model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new PoiCore model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new PoiCore();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing PoiCore model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing PoiCore model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the PoiCore model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return PoiCore the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = PoiCore::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}