Search code examples
phpyiiyii-extensionsyii-components

Yii form Text Field and CHtml Query: how to get single data by CHtml Query into Text Field


Hope you are all doing well.

I need some help from you regarding my question that how can I get single data by CHtml Query into Text Field.

for example I am using

<?php echo $form->dropDownList($model,'im_costprice', CHtml::listData(Purchaseorddt::model()->findAll(" pp_purordnum = '$pp_purordnum' "), 'pp_purchasrate', 'pp_purchasrate'), array('id'=>'purchasrate')); ?>

BUT I want to get single data into textfiled so that user can edit/ change the data. Such as:

<?php echo $form->textField($model,'im_costprice', CHtml::Data(Purchaseorddt::model()->findByAttributes(" pp_purordnum = '$pp_purordnum' "), 'pp_purchasrate', 'pp_purchasrate'), array('id'=>'purchasrate')); ?>

It is showing ERROR. Fatal error: Call to undefined method CHtml::Data()... How Can I solve this. Please help me with some idea.

Helps are highly appreciated .


Solution

  • Just store the value of the Purchaseorddt::model()->findByAttributes part in a separate variable and replace that variable in the place of your CHtml::Data section.

    Edit - Something on these lines would work. Do remember though that it is a better approach to do your queries in the controller or the model.

    <?php $x = Purchaseorddt::model()->findByAttributes( array('pp_purordnum' => $pp_purordnum));
           $y = $x['pp_purchasrate'];
           $model->im_costprice = $y;
           echo $form->textField($model,'im_costprice',array('id'=>'purchasrate'));   ?>