我试图从yii2 Listview中的另一个表中打印一个attribite,但我无法获取需要打印到listview中的数据
$query = Producto::find()->joinWith(['mPrecios']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 6,
],
]);
搜索模型中的数据提供者
$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
)
);
通过Controller发送DtaProvider
<?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>
答案 0 :(得分:1)
我假设您在模型中正确设置了关系,getMPrecios()。
如果这是一个hasOne(),您可以使用$model->mPrecios->attributeName
访问关系数据,如果它是hasMany(),则您必须执行
foreach($model->mPrecios as $mprecios)
{
echo $mprecios->attributeName;
}
无论您是否急切地加载它们(如使用joinWith())或延迟加载它们,都应该可以访问此关系值。