【问题标题】:Paginate related data in hasMany relationship在 hasMany 关系中对相关数据进行分页
【发布时间】:2013-04-03 22:46:05
【问题描述】:

我在尝试对 hasMany 关系中的相关数据进行分页时遇到了一些问题

我有两个模型 - Collection 和 Item 收藏 hasMany Item 项目属于收藏

在 Collection view.ctp 中,它显示了正在传递的 ID 的集合详细信息以及项目的相关数据。它从 find “first” 调用中接收此数据。

$this->set('collection', $this->Collection->find('first', $options));

生成的数组如下所示:

array(
    'Collection' => array(
        'id' => '4fc923f5-0a58-453f-9507-0c0c86106d80',
        'name' => '<collection_name>'
    ),

    'Item' => array(
        (int) 0 => array(
            'id' => '4fc92403-000c-4ed2-9dae-0c0c86106d80',
            'name' => 'Item1'
        ),
        (int) 1 => array(
            'id' => '4fc9241b-35ec-4810-b511-0c0c86106d80',
            'name' => 'Item2'
        ),
        (int) 2 => array(
            'id' => '4fc96e5d-ad74-4c05-a9da-0c0c86106d80',
            'name' => 'Item3'
        ),
        (int) 3 => array(
            'id' => '4fc96e64-a110-4036-bea2-0c0c86106d80',
            'name' => 'Item4'
        )
    )

现在我可以从这个分页调用中得到相同的结果,除了添加了 0 索引

$this->set('items', $this->paginate('Collection', array('Collection.id'=>$id)));

array(
    (int) 0 => array(

        'Collection' => array(
            'id' => '4fc923f5-0a58-453f-9507-0c0c86106d80',
            'name' => '<collection_name>'
        ),

        'Item' => array(
            (int) 0 => array(
                'id' => '4fc92403-000c-4ed2-9dae-0c0c86106d80',
                'name' => 'Item1'
            ),
            (int) 1 => array(
                'id' => '4fc9241b-35ec-4810-b511-0c0c86106d80',
                'name' => 'Item2'
            ),
            (int) 2 => array(
                'id' => '4fc96e5d-ad74-4c05-a9da-0c0c86106d80',
                'name' => 'Item3'
            ),
            (int) 3 => array(
                'id' => '4fc96e64-a110-4036-bea2-0c0c86106d80',
                'name' => 'Item4'
            )
      )
  )
);

我可以对返回的数组执行 array_shift,但是分页不起作用。我可以尝试为此创建一个自定义分页函数,但想知道是否有一种更简单的方法可以使用我正在使用的标准分页来执行此操作,因为它具有我想要的数据,只是额外的第一个数组父元素 [0]。

提前感谢您的任何建议。

【问题讨论】:

  • 嗯,分页是应该来做的吗?您正在对结果进行分页,这将通常返回一个列表记录。在您的情况下,“列表”仅包含一行(第 0 行)。但是,如果您的意图是对来自 Item 模型而不是 Collection 模型的记录进行分页,您应该修改您的分页并为 Item 模型分页。如果您需要一些帮助,请使用edit 链接发表评论或更新您的问题
  • 感谢您的回复。我将从项目更新分页。我能够将 'findType'=> 'first' 添加到 Collection 中的分页中,这给了我与 find 'first' 相同的数组,但 paginate 仍然无法在那里工作。我想我在尝试使用集合模型时遇到了这个错误,就像你说的那样,我应该从项目模型中做。再次感谢
  • 那是我的问题,但我也意识到我认为我的 foreach 搞砸了。再次感谢您的提示,让我更加兴奋。
  • 很高兴我能帮上忙。希望你能设法得到你想要的,否则发布一个新问题

标签: cakephp cakephp-2.3 pagination


【解决方案1】:
<?php

// Don't pull in child data, we'll fetch that manually.
$this->Collection->contain(); // Assuming you have this behavior.

// Get the collection.
$collection = $this->Collection->find('first', $options);

// Conditions
$this->paginate['Item'] = array(
    'conditions' => array(
        'Item.collection_id' => $collection['Collection']['id'];
    )
);

// Get the items
$items = $this->paginate('Item');

// Return to original child model structure.
array_walk($items, function(&$v) { // Anonymous functions requires PHP 5.3
    $v = $v['Item'];
});

// Add it to the collection array.
$collection['Item'] = $items;

?>

这就是我在子模型上实现分页的方式。试一试,也许对你也有用。

-A

【讨论】:

  • 感谢您的回复,是的,这就是我所做的。试图发布我自己的答案,但堆栈流不允许我这样做。 $this->paginate = array('Item' => array('recursive' => 1, 'limit' => 5, 'conditions' => array('Item.collection_id' => $id), 'order' => 'Item.name asc')
  • 刚刚调用的$this->set('items', $this->paginate('Item'));
  • 啊,我只添加了数组重组,所以我可以将它全部设置为一个变量。 :)
  • 是的,很好。没想到,以后可能会派上用场。
猜你喜欢
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
  • 2018-10-01
  • 2010-12-08
  • 2015-12-06
  • 1970-01-01
相关资源
最近更新 更多