Yii框架:分页有很多,属于

Yii框架:分页有很多,属于,yii,Yii,我整天都在面对这个问题 我有两张服务和日志表。每个服务可以有许多日志,每个日志属于一个服务。 我成功地为两者生成了CRUD功能。 以下是我得到的: app/models/Log.php /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class n

我整天都在面对这个问题

我有两张服务和日志表。每个服务可以有许多日志,每个日志属于一个服务。 我成功地为两者生成了CRUD功能。 以下是我得到的:

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>
服务:

注释

根据我早些时候与你的讨论,我认为这就是你想要的


不要使用CGridView,而是查看。它允许您在使用自己的数据模板的同时进行分页、Ajax检索等。它非常值得你花时间……

这正是我不想使用的:)然后看看这个:我也试过了。这不是我想要的方式。它显示了无限多的链接,虽然我只有5个,但这是什么方式?你在使用CActiveDataProvider吗?没有。我只是在检索通过foreach$model->logs循环的内容。基于两张桌子之间的关系。谢谢你的帮助。但我在尝试访问时得到了“尝试获取非对象的属性”,非常感谢。你救了我一天。这正是我想要的。如果浏览文档,很容易忽略一些细节。
<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; ?>