dtp0760 2016-06-20 13:23
浏览 65
已采纳

如何在yii2中将关系数据传递给listview

im trying to print an attribite from another table in yii2 Listview but im not able to get the data i need to print into the listview

   $query = Producto::find()->joinWith(['mPrecios']);


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

Data Provider in search model

    $searchModel = new ProductoSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $modelCliente=new Cliente;
        $cliente = Cliente::getClientes();
        $rows = Producto::autocomplete();//Se envia la variable que contiene los productos a la vista
            return $this->render('index', array(
                                                'rows'=>$rows,
                                                'model' => $model,
                                                'modelC'=>$modelCliente,
                                                'searchModel' => $searchModel,
                                                'clientes'=>$cliente,
                                                'listDataProvider' => $dataProvider
                                                )
                                );

Send DtaProvider through Controller

   <?php \yii\widgets\Pjax::begin(['id'=>'listaProductos','enablePushState' => true, 'timeout' => 8000000, 'clientOptions' => ['container' => 'pjax-container']]); ?>

            <?=

              ListView::widget([
                'options'=>[
                  'class'=>'col-lg-12  list-wrapper inline row-eq-height',
                ],

                'dataProvider' => $listDataProvider,
                'itemView' => function ($model, $key, $index, $widget) {
                    $itemContent = $this->render('_tabla_producto',['model' => $model]);
                    return $itemContent;
                },
                'itemOptions' => [
                    'tag' => false,
                ],
                'summary' => '',
                'layout' => '{items}{pager}',

                'pager' => [
                    'firstPageLabel' => 'Primera',
                    'lastPageLabel' => 'Ultima',
                    'maxButtonCount' => 5,
                    'options' => [
                        'class' => ' pagination col-xs-12'
                    ]
                ],
            ]);
          ?>
            <?php \yii\widgets\Pjax::end(); ?>

ListView

     <?php $IdProducto = $model['Refe'];?>
     <div id="producto-<?php echo $IdProducto;?>" class="content inline producto col-lg-4 box-body" >
           <div class="row">
             <div class="col-lg-6">
               <img class='img-responsive' src="<?php echo $model['ImagenProducto']?>"/>
             </div>
             <div class="row">
               <div class="col-lg-6" style="height:200px;">
                 <?php
                    if($model['Fraccion'] > 1){
                      echo "<button id='fraccion-$IdProducto' class='btn btn-danger btn-xs fraccionado' data-toggle='modal' data-target='#modalFraccion'><b>F</b></button>";
                    }
                    else{
                      echo "<button id='fraccion-$IdProducto' class='btn btn-default btn-xs disabled'><b>NF</b></button>";
                    }
                  ?><br>
                <b>Nombre:</b><br><?= $model['DescripcionCorta'] ?><br>
                <b>Presentacion:</b><br><?= $model['PresentacionCorta'] ?><br>
                <b>Precio:</b> HERE GOES THE OTHER TABLE ATTRIBUTE
              </div>
             </div>
           </div>
           <div class="row">
             <div class="col-lg-6">
               <div class="input-group">
                 <span class="input-group-btn" >
                   <button id="menos-<?php echo $IdProducto;?>" type="button" class="btn btn-sm btn-default btn-minuse" min='0' value="0">-</button>
                 </span>
                 <input id="input-<?php echo $IdProducto;?>"  type="text" value="0" class="form-control text-center" style="height:30px;">
                 <span class="input-group-btn">
                   <button id="mas-<?php echo $IdProducto;?>" type="button" class="btn btn-sm btn-default btn-pluss" min='0' value="0">+</button>
                 </span>
               </div>
             </div>
             <div class="col-lg-6 pull-left">
               <button id="anadir-<?php echo $IdProducto;?>"  type="button" class="text-center center-block btn btn-sm btn-default btn-add">Añadir</button>
             </div>
           </div>
         </div>
  • 写回答

1条回答 默认 最新

  • douzhan3900 2016-06-20 15:44
    关注

    I will assume you have your relation set up correctly in your model, getMPrecios().

    If this is a hasOne() you can access the relational data with $model->mPrecios->attributeName and if its a hasMany() you'll have to do a

    foreach($model->mPrecios as $mprecios)
    {
        echo $mprecios->attributeName;
    }
    

    This relational values should be accessible regardless of whether you eagerly load them (like you do with a joinWith()) or lazy load them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题