2012-07-10 77 views
0

我一直在面對這個問題整天。Yii框架:分頁與HAS_MANY,BELONGS_TO

我有2個表服務和日誌。每個服務可以有多個日誌,每個日誌都屬於一個服務。 我設法爲兩者生成CRUD功能。 這裏是我的了:

應用程序/模型/ 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'), 
    ); 
} 

應用程序/模型/ 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'), 
    ); 
} 

應用程序/ 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> 

<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; ?> 

的問題是:我如何去在 'foreach循環' 分頁的結果? 謝謝

+0

你可以使用cgridview – Orlymee 2012-07-10 14:48:46

+0

這正是我不想使用:) – Madi 2012-07-10 14:54:15

+0

然後看看這個:http://www.yiiframework.com/doc/api /1.1/CPagination – Orlymee 2012-07-10 14:55:42

回答

0

取而代之的是CGridView的,看看docs for CListView。它允許你做分頁,Ajax檢索等,同時也使用你自己的模板來處理數據。這是值得的你的時間...

+0

非常感謝。你拯救了我的一天。這正是我所期待的。如果你正在通過文檔飛行,那麼很容易忽略一些細節。 – Madi 2012-07-11 06:25:10

0

基於我之前與你的討論,我認爲這就是你以後。

Yii Simple Pagination

+0

感謝您的幫助。但是,當嘗試訪問<?php $ this-> breadcrumbs = array( 'Service'=> array('index'), $ model-> ident_nr, (嘗試訪問非對象的屬性) ); – Madi 2012-07-11 05:45:17