0

I'm trying to print data with the help of kartik mPDF. The detailview part is printing without any issue. But I need to print some lines along with the detailview. Now I need to pass data from the model to the view to be printed. I'm trying to do that but not so sure. Currently I'm getting error -

Object of class yii\db\ActiveQuery could not be converted to string

I may be wrong in my approach. But please tell me what correction needs to be done. This is the same question as - How to pass data from model to a view in yii2

Code of Controller Action -

public function actionPrintsalarystatement($id) {

        //Yii::app()->params['period'] = $period;
        //$s_period = $this->originalperiod;
        $period = Salary::find()->select(['s_period'])->where(['s_id' => $id])->asArray();
        //$period = Yii::$app->request->post('period');
        //$this->period = Salary::find()->select(['s_period'])->where(['s_id' => $id]);
        //$period = ArrayHelper::map(Salary::find($id)->select(['s_period'])->asArray()->all(), 's_period', 'period'),
        //$model =  Salary::find()->where(['s_period' => $period]);
        $model = $this->findModel($id);
        $searchModel  = new SalarySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $data         = Salary::findOne($id);
        $content = $this->renderPartial('_printSalarystatement', [
            'model' => $model,
            'dataProvider' => $dataProvider,
            'searchModel'  => $searchModel,
            'data'=> $data,
            'period' => $period,

            ]);
        $pdf = new Pdf([
            'mode'=> Pdf::MODE_UTF8,
            'format'=> Pdf::FORMAT_A4,
            'destination'=> Pdf::DEST_BROWSER,
            //'destination' => Pdf::DEST_DOWNLOAD,
            'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
            // any css to be embedded if required
            'cssInline' => '.kv-heading-1{font-size:18px}', 
             // set mPDF properties on the fly
            'options' => ['title' => 'Print Payslip'],
             // call mPDF methods on the fly
            'methods' => [
                'SetHeader'=>['Private and Confidential'], 
                'SetFooter'=>['This Payslip is computer generated.'],
            ],
            'content' => $content,

        ]);
        return $pdf->render();
        //return $this->render('_printSalarystatement', ['s_period' => $s_period]);

    }

Updated view.php

    <?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\web\View;

/* @var $this yii\web\View */
/* @var $model frontend\modules\salary\models\Salary */
//@var $model yii\base\Model
//@var $totaldays any

//$this->title = $model->s_id;
//$this->period = $model->s_period;
$this->params['breadcrumbs'][] = ['label' => 'Salaries', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="salary-view">


    <h1><strong><p class="text-center">My Company</p></strong></h1>
    <p class="text-center">Pay Slip for the month of <?php $model['s_period'];?> </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            's_id',
            's_date',
            's_period',
            's_empid',
            's_empname',
            's_workingdays',
            's_leave',
            's_holiday',
            's_wageperday',
            's_totalwage',
            's_ovthour',
        ],
    ]) ?>

</div>

What I'm trying to do - enter image description here

Currently I'm getting enter image description here

enter image description here

6
  • in which line you are getting this error? Commented Jul 18, 2016 at 13:23
  • Change <p class="text-center">Pay Slip for the month of <?php echo $period;?> </p> to <p class="text-center">Pay Slip for the month of <?php echo $model->s_period;?> </p> in your view file. Commented Jul 18, 2016 at 13:24
  • $period = Salary::find()->select(['s_period'])->where(['s_id' => $id])->asArray()->one(); or ->all() Commented Jul 18, 2016 at 13:25
  • You don't even need to get $period model in your controller. Your $model has already loaded the object. Just change the view and remove $period = ... ActiveQuery object from your controller. Commented Jul 18, 2016 at 13:27
  • @ThinkDifferent If I remove $period ActiveQuery object from my the controller action - I get error Undefined variable: period. But If I keep that and change my view file to - <?php echo $model->s_period;?> - the error is gone But I get output like "Pay Slip for the month of ". The period is not passed there. Please let me know what to do. Commented Jul 18, 2016 at 13:36

1 Answer 1

1

Comment the $period object you are creating in controller as follows:

public function actionPrintsalarystatement($id) {

    //Yii::app()->params['period'] = $period;
    //$s_period = $this->originalperiod;
    //$period = Salary::find()->select(['s_period'])->where(['s_id' => $id])->asArray(); <=== Comment this line
    //$period = Yii::$app->request->post('period');
    //$this->period = Salary::find()->select(['s_period'])->where(['s_id' => $id]);
    //$period = ArrayHelper::map(Salary::find($id)->select(['s_period'])->asArray()->all(), 's_period', 'period'),
    //$model =  Salary::find()->where(['s_period' => $period]);
    $model = $this->findModel($id);
    $searchModel  = new SalarySearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    $data         = Salary::findOne($id);
    $content = $this->renderPartial('_printSalarystatement', [
        'model' => $model,
        'dataProvider' => $dataProvider,
        'searchModel'  => $searchModel,
        'data'=> $data,
        //'period' => $period,  <=== Comment this as well

        ]);
    $pdf = new Pdf([
        'mode'=> Pdf::MODE_UTF8,
        'format'=> Pdf::FORMAT_A4,
        'destination'=> Pdf::DEST_BROWSER,
        //'destination' => Pdf::DEST_DOWNLOAD,
        'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
        // any css to be embedded if required
        'cssInline' => '.kv-heading-1{font-size:18px}', 
         // set mPDF properties on the fly
        'options' => ['title' => 'Print Payslip'],
         // call mPDF methods on the fly
        'methods' => [
            'SetHeader'=>['Private and Confidential'], 
            'SetFooter'=>['This Payslip is computer generated.'],
        ],
        'content' => $content,

    ]);
    return $pdf->render();
    //return $this->render('_printSalarystatement', ['s_period' => $s_period]);

}

And in your view do the following:

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\web\View;

/* @var $this yii\web\View */
/* @var $model frontend\modules\salary\models\Salary */
//@var $model yii\base\Model
//@var $totaldays any

//$this->title = $model->s_id;
//$this->period = $model->s_period;
$this->params['breadcrumbs'][] = ['label' => 'Salaries', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="salary-view">
<h1><strong><p class="text-center">My Company</p></strong></h1>
<p class="text-center">Pay Slip for the month of <?php echo $model['s_period'];?> </p>

<?php 
//Add these lines to see what you get in your $Model object
echo '<pre>'; print_r($model); echo '</pre>'; ?>

<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        's_id',
        's_date',
        's_period',
        's_empid',
        's_empname',
        's_workingdays',
        's_leave',
        's_holiday',
        's_wageperday',
        's_totalwage',
        's_ovthour',
    ],
]) ?>
Sign up to request clarification or add additional context in comments.

11 Comments

I've tried as you said in this answer. Please have a look what output I'm getting. I've addded it in the question.
ok in your view do print_r($model); and see if you get the object values.
Not so sure where to put this code. Please tell me where to put it.
I have updated the answer, copy the lines in the view file and place it there in your file, then let me know what it displays.
I've added <?= print_r($model); ?> after <p class="text-center">Pay Slip for the month of <?php $model["s_period"];?> </p>. Got some output. I'm adding it in the question. Please have a look. I've added.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.