0

我有两个模型:PlansPlanDetails

关系是:PlanDetails hasMany Plans计划属于PlanDetails 。

PlanDetails view.ctp中,我正在拉入相关的Plans

我正在尝试按任何字段对计划进行排序(我已经全部尝试过),但我无法让它工作。我假设我忽略了一些非常简单的事情。

这是我的基本代码:

计划详情 >> view.ctp:

...foreach ($planDetail['Plan'] as $plan_edit) :
$class = null;
if ($i++ % 2 == 0) {
    $class = ' class="altrow"';
}...

<?php echo $this->Paginator->sort('Plan ID', 'Plan.id'); ?>...

...<?php echo $plan_edit['id']; ?>

plan_details_controller.php:

...function view($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid plan detail', true));
        $this->redirect(array('action' => 'index'));
    }
    $this->PlanDetail->recursive = 2; // To run the editable form deeper.
    $this->set('planDetail', $this->PlanDetail->read(null, $id));
    $this->set('plan', $this->paginate('Plan'));
}...

我应该补充一点,没有抛出任何错误,并且 ID 字段上的 sort() 箭头按预期显示,但是当单击任何一种方式时,排序顺序都不会改变。

4

2 回答 2

1

抱歉,我无法对问题本身发表评论,但我注意到在您的操作中,您设置planDetail为您阅读的 PlanDetail 记录(recursive设置为 2),然后您设置planpaginate称呼。

然后,在您的视图模板中,您将迭代$planDetail' 包含的Plan关联,如下所示:

foreach ($planDetail['Plan'] as $plan_edit):

但是为了完成排序和分页,您需要显示paginate调用的结果,即遍历包含在$plan.

在您的视图模板中执行 adebug($plan)以查看您在那里获得的结果,并查看当您按不同字段排序时记录的顺序是否发生变化。

此外,也许您正在使用我不知道的语法,但如果您只是调用$this->paginate('Plan')您的控制器,我不知道您只会获取Plan特定记录的相关记录PlanDetail。(没有任何东西将$id传递给您的view操作与Plan记录联系起来。)您可能需要在paginate调用中添加一些条件,如下所示:

$this->paginate['Plan'] = array('conditions' => array('Plan.plan_detail_id' => $id));
$this->set('plans', $this->paginate('Plan'));
于 2011-03-24T10:02:33.257 回答
0

这是我为解决这个问题所做的。基于 johnp & tokes 的一些有用指导。

计划细节/view.ctp:

...$i = 0;
foreach ($plan as $plan_edit) : // note $plan here.
}...

在我的 plan_details_controller.php 视图操作中:

$conditions = array("Plan.plan_detail_id" => "$id");
$this->set('plan', $this->paginate('Plan', $conditions)); // note "plan" in the first argument of set (this is required to pass the results back to the view). Also. The $condition is set to return ONLY plans that match the plan_detail_id of id (on the plan_details table).

在我看来,为了获得我的结果(因为我更改了数组名称),我必须将获取值的方式更改为:

$plan_edit['Plan']['modified'] // note I placed ['Plan'] in front of these calls as the array called for this to get the data...

好吧,直到下一个问题!解决了。

于 2011-03-25T21:56:32.550 回答