I have a textfield textfield1
in my form with an automatically filled value. I want to make a function in my model to generate the code for a textfield2
as ID. And to generate that code, in the my model I need to read from the database with the following query:
select * table_a where code=***value from textfield1***
This value is located in view/_form. How can I pass the value from view or _form to the model?
My View/Form :
// this is the textfield that is filled automatically. I want to it to the model
<div class="row">
<?php echo $form->labelEx($model,'kode'); ?>
<?php echo $form->textField($model,'kode',array('size'=>40,'maxlength'=>255)); ?>
<?php echo $form->error($model,'kode'); ?>
</div>
// this is the text field where I want to fill by query in my model
<div class="">
<?php echo $form->labelEx($model,'kode_primary'); ?>
<?php echo $form->textField($model,'kode_primary',array('size'=>40,'maxlength'=>255,'value'=>(($model->isNewRecord)? $model->generateKode_P():$model->no),'readOnly'=>'true')); ?>
<?php echo $form->error($model,'kode_primary'); ?>
My Model :
public function generateKode_P(){
$connection = yii::app()->db;
$command=$connection->createCommand("select * from arsip_perihal WHERE kode='I want to get value from another textfield where has generated value located in same view/_Form'");
}
first you have to submit the form, because the server doesn't know about that value, then in your controller catch that value, similar to default actionCreate()
and actionUpdate()
functions of your controller.
because you have used active inputs, for example if your models name is User
, the value will probably be in a $_POST['User']['kode_primary']
or $_GET['User']['kode_primary']
variable, depending on what your forms sending method is
it's best to grab that variable by yii
s default function:
$user_attr = Yii::app()->request->getParam('User');
// now you can access it like
$user_attr['kode_primary'];