Yii Framework Forum: Cpaginate : Pagination with HAS_MANY, BELONGS_TO - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cpaginate : Pagination with HAS_MANY, BELONGS_TO Rate Topic: -----

#1 User is offline   Madi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 06-March 12
  • Location:Germany

Posted 10 July 2012 - 06:33 AM

Hi,

I have been facing this problem all day long.

I have 2 tables Service and Log. Each Service can have many Logs and each Log belongs to a Service.
I managed to generate the CRUD functionality for both.
Here is what I got:

app/models/Log.php
/**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array
        (
            'services' => array(self::BELONGS_TO, 'Service', 'sv_ident_nr'),
        );
    }

app/models/Service.php
/**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array
        (
            'logs' => array(self::HAS_MANY, 'Log', 'sv_ident_nr'),
        );
    }


app/controlelrs/ServiceController.php
    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id)
    {
        $this->render('view', array
        (
            'model' => $this->loadModel($id),
        ));
    }


app/views/service/view.php

<h1>Service: <?php echo $model->ident_nr; ?></h1>

<table class="dataGrid">
    <tr>
        <th class="label"><?php echo CHtml::encode($model->getAttributeLabel('proj_nr')); ?></th>
        <td><?php echo CHtml::encode($model->proj_nr); ?></td>
    </tr>
    <tr>
        <th class="label"><?php echo CHtml::encode($model->getAttributeLabel('customer')); ?></th>
        <td><?php echo CHtml::encode($model->customer); ?></td>
    </tr>
</table>
<h1>Comments</h1>
<?php
foreach ($model->logs as $log): ?>
    <div class="comment" id="c<?php echo $log->id; ?>">
        <div class="actions">
            <?php 
                echo CHtml::link('View',array($this->createUrl('../log/view', array('id'=>$log['id']))));
                echo('&nbsp');
                echo CHtml::link('Update',array($this->createUrl('../log/update', array('id'=>$log['id']))));
                echo('&nbsp');
                echo CHtml::link('Delete',array($this->createUrl('../log/delete', array('id'=>$log['id']))));
            ?>
        </div>
        <div class="author">
            <?php 
                echo $log->logger;
            ?>
        </div>
        <div class="time">
            <?php echo date('j F Y \a\t h:i a', $log->created_at); ?>
        </div>

        <div class="content">
            <?php echo nl2br(CHtml::encode($log->comment)); ?>
        </div>

    </div><!-- comment -->
<?php endforeach; ?>


The question is : How do I go about paginating the results in the 'foreach loop'?
Thanks
0

#2 User is offline   Madi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 06-March 12
  • Location:Germany

Posted 11 July 2012 - 01:50 AM

I got this solved through using CListView and a CArrayDataProvider.

/app/views/service/view.php
<?php
$dataProvider = new CArrayDataProvider($rawData = $model->comments);

$this->widget('zii.widgets.CListView', array
(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_comment',   // refers to the partial view named '_comment'
));


I also added a comement view file

/app/views/service/_comment.php
<div class="comment">
    <div class="author">
        <?php echo CHtml::encode($data->logger); ?>
    </div>
    <div class="time">
        <?php echo CHtml::encode(date('j F Y \a\t h:i a',$data->created_at)); ?>
    </div>
    <div class="content">
        <?php echo CHtml::encode($data->content); ?>
    </div>
</div>


CListView is highly customisable and you could use it to do just about everything you want.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users