Model yii2-将字段添加到模型中而不添加新的tablefield
我想在模型中添加一个字段,这样它就会显示在index/update/create中 它也应该在更新/创建中可编辑 在sendin/save from update/create之后,我想处理这个字段的内容,计算一些内容,然后将它与所有其他字段(它们在DB中有相应的字段)一起写入现有DB字段中的数据库 我可以通过添加模型(frontend/model/poi.php)在索引中显示一个新字段Model yii2-将字段添加到模型中而不添加新的tablefield,model,field,yii2,Model,Field,Yii2,我想在模型中添加一个字段,这样它就会显示在index/update/create中 它也应该在更新/创建中可编辑 在sendin/save from update/create之后,我想处理这个字段的内容,计算一些内容,然后将它与所有其他字段(它们在DB中有相应的字段)一起写入现有DB字段中的数据库 我可以通过添加模型(frontend/model/poi.php)在索引中显示一个新字段 public $tempVal = NULL; 和在同一文件中的规则中 [['tempVal'], 'st
public $tempVal = NULL;
和在同一文件中的规则中
[['tempVal'], 'string', 'max' => 64],
因此,此字段中的所有行都将具有相同的内容。
->那不是我想要的
字段在每行中应有不同的内容(如随机数)
如何解决此问题?这是模型和控制器(不工作) /前端/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.');
}
}
}
请显示POI的型号和控制器