yii2 - 将字段添加到没有新表格的模型
问题描述:
我想向模型添加字段,因此它显示在索引/更新/创建中。yii2 - 将字段添加到没有新表格的模型
它也应该可以在update/create中编辑。
在sendin/save from update/create后,我想要处理这个字段的内容,计算一些东西,然后将它与所有其他字段(它们在DB中具有对应的字段)一起写入到现有DB字段中的数据库。
我可以通过在模型中加入(前端/模型/ poi.php
public $tempVal = NULL;
和规则相同的文件
[['tempVal'], 'string', 'max' => 64],
因此,所有的行会显示在指数的新领域 - >那不是我想要的
该字段在每一行中应该有不同的内容(比如说随机数)
我该如何解决这个问题?
答
这里是模型和控制器(不工作)
/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.');
}
}
}
请出示模型和POI的控制器 – scaisEdge