类yii \ db \ ActiveQuery的对象无法转换为字符串

时间:2016-07-18 13:16:23

标签: yii2

我试图在kartik mPDF的帮助下打印数据。 detailview部分正在打印,没有任何问题。但是我需要打印一些细线和细节视图。现在我需要将数据从模型传递到要打印的视图。我试图这样做但不太确定。目前我收到错误 -

api-key

我的方法可能有问题。但请告诉我需要做哪些更正。这与 - How to pass data from model to a view in yii2

的问题相同

控制器行动守则 -

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

更新了view.php

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]);

    }

我想做什么 - enter image description here

目前我正在接受 enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

注释您在控制器中创建的$ period对象,如下所示:

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]);

}

在您看来,请执行以下操作:

<?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',
    ],
]) ?>