use yii::app()->user->id in a form and to save the username in table

i need to use yii::app()->user->id in a form and to save the username in table

<?php echo $form->labelEx($model,‘User’); ?>

&lt;?php echo &#036;form-&gt;textField(&#036;model,'User',array('size'=&gt;15,'maxlength'=&gt;15)); ?&gt;


&lt;?php echo &#036;form-&gt;error(&#036;model,'User'); ?&gt;

i replaced this with

<?php echo $form->labelEx($model,‘User’); ?>

&lt;?php echo &#036;form-&gt;textField(&#036;model,'User','yii::app()-&gt;user-&gt;id'); ?&gt;


&lt;?php echo &#036;form-&gt;error(&#036;model,'User'); ?&gt;

but it didnt work so i replaced to

<?php echo $form->labelEx($model,‘User’); ?>

&lt;?php echo yii::app()-&gt;user-&gt;id' ?&gt;


&lt;?php echo &#036;form-&gt;error(&#036;model,'User'); ?&gt;

now its printing the username in form but not stored in db table…is there any other suggestion help me .thanks in advance

i think there is no need echo UserId in your form if id column is auto incremented & pk in database ,you can try this way …

<?php echo $form->textField($model,‘UserNameAttributesColumn’,); ?> also be sure to initialize the user model in your Controller action method like ,

$form = new UserModel;and pass your model in renderMethod as ‘form’=> $form.

After a long try i found answer in simple way…posting the code for other need


<div class="row">

		<?php //echo $form->labelEx($model,'User'); ?>

		<?php //echo $form->yii::app()->user->id(array('model'=>$model,'attribute'=>'User','value'->$model->User,)); ?>

		

	<input size="15" maxlength="15" name="User[User]" id="User_user" type="hidden" value="<?php echo yii::app()->user->id ?>"  />	




		<?php echo $form->error($model,'User'); ?>

	</div>



thank you

Why would you put that in the view as a hidden column at all ?

If that value cannot be edited, than why put it there in the first place… you can assign that value in your code with


$model->user = yii::app()->user->id;

just before saving the record… in this case the value is always assigned by your code and there is no need for a validation on that field at all…