Search code examples
phpyii

Send controller data into form field in Yii


UPDATED THE SOLUTION::: IT IS WORKING NOW

I have created a controller with join query. Now I want to include controller to actionCreate function and show data into text filed in form.

Controller

public function actionCreate()
{
    $model=new Grndetail;

    if (isset($_POST['Grndetail'])) {
        $model->attributes=$_POST['Grndetail'];
        if ($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }
    
    $grndata = $this->GrnData1();
    $model->im_grnnumber = $grndata['im_grnnumber'];
    $this->render('create',array('model'=>$model, 'grndata'=>$grndata,));
}



public function GrnData1()
{               
    $sql = Yii::app()->db->createCommand()
              ->select('t.im_grnnumber, t.im_purordnum, r.pp_purordnum, r.cm_code, r.pp_quantity, r.pp_unit, r.pp_unitqty, r.pp_purchasrate, p.cm_description, p.cm_code')
              ->from('im_grnheader t')
              ->join('pp_purchaseorddt r', 't.im_purordnum = r.pp_purordnum')
              ->join('cm_productmaster p', 'p.cm_code = r.cm_code')
             //->where('id=:id', array(':id'=>$id))
              ->order('im_grnnumber DESC')
              ->queryRow();
                    
     return $sql;
}

form filed

<?php echo $form->textField($model,'im_grnnumber'); ?>

Is anything I am missing. How can I view the data as value in form when I am creating something.

UPDATED THE SOLUTION::: IT IS WORKING NOW


Solution

  • You should change your program as

    public function GrnData1(){
    
    $sql = Yii::app()->db->createCommand()->setFetchMode(PDO::FETCH_OBJ) // change it
    
    ->select('t.im_grnnumber, t.im_purordnum, r.pp_purordnum, r.cm_code, r.pp_quantity,  r.pp_unit, r.pp_unitqty, r.pp_purchasrate, p.cm_description, p.cm_code')
    
    ->from('im_grnheader t')
    ->join('pp_purchaseorddt r', 't.im_purordnum = r.pp_purordnum')
    ->join('cm_productmaster p', 'p.cm_code = r.cm_code')
    ->order('im_grnnumber DESC')
    ->queryRow();
    
    Return $sql; // write here return statement
    
    }
    

    Now you can access your data like this

    echo $form->textField($model,'im_grnnumber', array('value'=>$grndata->im_grnnumber));
    

    Hope, It will help you.

    Thanks