Yii Framework Forum: Form fields names are different from database table column names - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Form fields names are different from database table column names Rate Topic: -----

#1 User is offline   Blue Sapphire 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-February 10

Posted 11 May 2010 - 02:53 PM

I have following code in form. Kindly see names of form fields

 <?php

         $form = $this->beginWidget('CActiveForm', array(
                                'id' => 'user-form',
                                'action' => 'create',
                                'enableAjaxValidation' => false,
                        ));
    ?>
    <p class="note">Fields with <span class="required">*</span> are required.</p>
    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model, 'Username');?>
        <?php echo $form->textField($model, 'wausername', array('size'=>60, 'minlength' => 3 , 'maxlength' => 128, 'id' => 'myUsernameID', 'name' => 'myUsername'));?>
        <?php echo $form->error($model, 'wausername', array('name' => 'waErrorMessage', 'id' => 'waErrorMessageID'));?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'Password');?>
        <?php echo $form->textField($model, 'wapassword', array('size' => 60, 'maxlength' => 128, 'id'=> 'myPasswordID', 'name'=> 'myPassword'));?>
        <?php echo $form->error($model, 'wapassword', array('name' => 'waPasswordErrorMEssage', 'id' => 'waPasswordErrorMEssageID'));?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('id' => 'myID', 'name' => 'mySubmitName'));?>
    </div>

    <?php $this->endWidget(); ?>



Following is table schema :

--
-- Table structure for table `watable`
--

CREATE TABLE IF NOT EXISTS `watable` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `wausername` varchar(128) NOT NULL,
  `wapassword` varchar(128) NOT NULL,
  `waemail` varchar(128) NOT NULL,
  `wadetail` varchar(128) NOT NULL, 
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;



You can see that form fields are different from column names. How can I insert new record in database by using YII CActiveRecord.Or is there any other class in YII for this purpose.

Thanks in advance
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 12 May 2010 - 04:02 AM

Active record is the correct tool to use, you have just to change approach.

In the view you have to write
 <?php echo $form->labelEx($model, 'wausername');?>


and use attributeLabel for change the text of the label

	public function attributeLabels()
	{
		return array(
			'wausername'=>'Username', // for internationalized sites: Yii::t('user', Username)
		);
	}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users