在yii2中显示来自模型的数据,没有任何关系

时间:2016-09-02 07:32:57

标签: yii2

我有两个模型1. Orders 2. Offers。他们之间没有任何关系。 在Order的视图文件中,我想显示of_name表的最新条目中的of_descOffer。 我的报价searchModel看起来像是:

public function search($params)
    {
        $query = Offers::find()->orderBy(['of_id' => SORT_DESC])->one();;

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'of_id' => $this->of_id,
        ]);

        $query->andFilterWhere(['like', 'of_name', $this->of_name])
            ->andFilterWhere(['like', 'of_description', $this->of_description]);

        return $dataProvider;
    }

Order的控制器操作中,我试过了 -

public function actionPrintorders($id) {

        $model = $this->findModel($id);
        $searchModel  = new OrdersSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $data         = Orders::findOne($id);

        $searchModel1 = new OffersSearch();
        //$dataProvider1 = $searchModel1->search(Yii::$app->request->queryParams);
        $content = $this->renderPartial('_printorder', [
            'model' => $model,
            'dataProvider' => $dataProvider,
            'searchModel'  => $searchModel,
            'data'=> $data,
            'searchModel1' => $searchModel1,
            //'dataProvider1' => $dataProvider1,
            //'model2' => $model2,
            //'period' => $period,

            ]);
        $footer = "<table name='footer' width=\"1000\">
           <tr>
             <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\"><u>Working Hours : </u></td>
           </tr>
           <tr>
             <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">9:00am to 9:00pm</td>
           </tr>
           <tr>
             <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">Friday Morning Closed</td>             
             <td style='font-size: 18px; padding-bottom: 20px;' align=\"right\">Prop:      M.Sinha</td>
           </tr>
         </table>";
        $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 Orderslip'],
            //'options' => ['defaultfooterline' => 0,],
             // call mPDF methods on the fly
            'methods' => [
                'SetHeader'=>['Amit Optical'], 
                'SetFooter'=>[$footer],
            ],
            'content' => $content,

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

    }

并尝试在订单视图中传递值,如 -

<h3 style="margin-bottom:0;margin-top:2;margin-left:2;"><strong><p class="text-center"><?php echo $searchModel1['of_name'];?></p></strong></h3>

没有错误,但也没有这样的输出。请告诉我怎么做。

更新 我已尝试过此处制作的解决方案Yii2 render two models in one view 但显然卡在$key。不知道如何传递它。 我现在的控制器动作如下所示。

public function actionPrintorders($id) {

        $model = $this->findModel($id);
        $searchModel  = new OrdersSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $data         = Orders::findOne($id);

        $searchModel1 = new OffersSearch();
        $modelOffer  = Offers::findOne($key);
        //$dataProvider1 = $searchModel1->search(Yii::$app->request->queryParams);
        $content = $this->renderPartial('_printorder', [
            'model' => $model,
            'dataProvider' => $dataProvider,
            'searchModel'  => $searchModel,
            'data'=> $data,
            'searchModel1' => $searchModel1,
            'modelOffer'=>$modelOffer,
            //'dataProvider1' => $dataProvider1,
            //'model2' => $model2,
            //'period' => $period,

            ]);
        $footer = "<table name='footer' width=\"1000\">
           <tr>
             <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\"><u>Working Hours : </u></td>
           </tr>
           <tr>
             <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">9:00am to 9:00pm</td>
           </tr>
           <tr>
             <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">Friday Morning Closed</td>             
             <td style='font-size: 18px; padding-bottom: 20px;' align=\"right\">Prop:      M.Sinha</td>
           </tr>
         </table>";
        $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 Orderslip'],
            //'options' => ['defaultfooterline' => 0,],
             // call mPDF methods on the fly
            'methods' => [
                'SetHeader'=>['Amit Optical'], 
                'SetFooter'=>[$footer],
            ],
            'content' => $content,

        ]);
        return $pdf->render();

    }

1 个答案:

答案 0 :(得分:0)

错误发生在代码的开头,它应该是

public function search($params)
    {
        $query = Offers::find()->orderBy(['of_id' => SORT_DESC]);

在$ query var的末尾没有 - &gt; one(),因为ActiveDataProvider需要\ yii \ db \ ActiveQuery对象。