V
V
Vyacheslav_Shilov2020-07-30 11:29:33
Yii
Vyacheslav_Shilov, 2020-07-30 11:29:33

Yii1.1. How to display data from linked table in CListView?

Yii1.1. How to display data from linked table in CListView?
Action code:

public function actionIndex()
    {
        $firsts = Firsts::model()->with('seconds')->findByPk(1);
        $firstss = Firsts::model()->with('seconds')->findAll();
        foreach ($firstss as $second)
            echo $second->name. "<br>";
        $criteria = new CDbCriteria;

        //  $criteria->condition = 'id > 2';


        //  $criteria->order = 'name ASC';

        $dataProvider=new CActiveDataProvider('Firsts',array(
            'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>9,
            )
        ));
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
            'firsts'=>$firsts,
            'firstss'=>$firstss,
        ));
    }


View action code:
<?php
/* @var $this AllController */
/* @var $dataProvider CActiveDataProvider */
?>
<?php $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
    //'emptyText'
    'sorterHeader'=>'Сортировать по: ',
    'summaryText' => '{start} - {end} из {count}',
    'sortableAttributes'=>array('name', 'id', 'lastname'),
));
foreach ($firsts->seconds as $second)
    echo $second->name. "<br>";

?>

Listview code:
<?php
/* @var $this FirstsController */
/* @var $data Firsts */
?>

<div class="view">

  <b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
  <?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>
  <br />

  <b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>
  <?php echo CHtml::encode($data->name); ?>
  <br />

  <b><?php echo CHtml::encode($data->getAttributeLabel('lastname')); ?>:</b>
  <?php echo CHtml::encode($data->lastname); ?>
  <br />

  <b><?php echo CHtml::encode($data->getAttributeLabel('phone')); ?>:</b>
  <?php echo CHtml::encode($data->phone); ?>
  <br />

    <b><?php echo CHtml::encode($data->getAttributeLabel('sd')); ?>:</b>
    <?php echo CHtml::encode($data->seconds->name); ?>
    <br />



</div>
<?php
//    foreach ($firsts->seconds as $second)
//        echo $second->name. "<br>";
   // echo $firsts->seconds->name;
//    foreach ($firsts->name as $second)
//        echo $second. "<br>";
?>


It is necessary that the 'name' field of the linked table is displayed in the 'Sd' attribute.
Nothing is displayed there.
5f2284b316f14674863388.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question