jayanthan
(Jayanthan Webmaker)
1
i need to use yii::app()->user->id in a form and to save the username in table
<?php echo $form->labelEx($model,‘User’); ?>
<?php echo $form->textField($model,'User',array('size'=>15,'maxlength'=>15)); ?>
<?php echo $form->error($model,'User'); ?>
i replaced this with
<?php echo $form->labelEx($model,‘User’); ?>
<?php echo $form->textField($model,'User','yii::app()->user->id'); ?>
<?php echo $form->error($model,'User'); ?>
but it didnt work so i replaced to
<?php echo $form->labelEx($model,‘User’); ?>
<?php echo yii::app()->user->id' ?>
<?php echo $form->error($model,'User'); ?>
now its printing the username in form but not stored in db table…is there any other suggestion help me .thanks in advance
ismail
(Mdismail N001)
2
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.
jayanthan
(Jayanthan Webmaker)
3
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
mdomba
(Maurizio Domba Cerin)
4
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…