我是yii框架的新手。我正在使用yii框架进行更新操作。我的控制器名称为sitecontroller.php,model jobseekerprofile.php,view personal.php。我的表是job_seeker_profile,包含字段id,user_id,contact_no,gender,dob,mstatus,address。我无法通过表单发布更新数据。
我的控制器是 sitecontroller.php
class SiteController extends Controller
{
public function actionpersonal()
{
$user_id = trim($_GET['id']);
$model = new jobseekerprofile();
$model = jobseekerprofile::model()->find(array(
'select' => 'contact_no,address', "condition" => "user_id=$user_id",
'limit' => 1,));
$model = $this->loadModel($user_id);
if (isset($_POST['jobseekerprofile'])) {
$model->attributes = $_POST['jobseekerprofile'];
if ($model->save()) {
$this->redirect(array('profile', 'user_id' => $model->user_id));
}
}
$this->render('personal', array('model' => $model));
}
public function loadModel($user_id)
{
echo $model = jobseekerprofile::model()->findByPk($user_id);
if ($model === null)
throw new CHttpException(404, 'The requested page does not exist.');
return $model;
}
}
查看-personal.php
<div class="form">
<?php $form = $this->beginWidget('CActiveForm', array(
'id' => 'login-form',
'enableClientValidation' => false,
'htmlOptions' => array(),
'clientOptions' => array(
'validateOnSubmit' => true
),
)); ?>
<?php
foreach (Yii::app()->user->getFlashes() as $key => $message) {
echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
}
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model, 'Contact No'); ?>
<?php echo $form->textField($model, 'contact_no'); ?>
<?php echo $form->error($model, 'contact_no'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'Address'); ?>
<?php echo $form->textField($model, 'address'); ?>
<?php echo $form->error($model, 'address'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Save'); ?>
</div>
<?php $this->endWidget(); ?>
模型jobseekerprofile.php
rules in model
public function rules()
{
return array(
array('contact_no,gender,dob,address,mstatus','required'),
);
}
有人帮帮我吗?
答案 0 :(得分:0)
您需要使用大写字母。
Jobseekerprofile.php和$model = new Jobseekerprofile();
至少,我甚至可以称之为:JobseekerProfile.php和$model = new JobseekerProfile();
或其他什么。
示例控制器(未测试):
public function actionPersonal($id) {
$model = Jobseekerprofile::model()->findByPk($id);
if (isset($_POST['Jobseekerprofile'])) {
$model->attributes = $_POST['Jobseekerprofile'];
if ($model->save())
$this->redirect(array('view', 'id' => $model->id));
}
$this->render('personal', array(
'model' => $model,
));
}
此外,您可以将Gii posibilities of Yii用于这些标准表单。它将根据您的数据库及其中的关系生成表单。