Cakephp 在hasMany关系中对相关数据进行分页

Cakephp 在hasMany关系中对相关数据进行分页,cakephp,cakephp-2.3,paginate,Cakephp,Cakephp 2.3,Paginate,我在尝试为hasMany关系中的相关数据分页时遇到了一些问题 我有两个模型-收集和项目 收藏有很多物品 属于收藏的物品 在Collection view.ctp中,它显示正在传递的ID的集合详细信息以及项的相关数据。它从find“first”调用接收此数据 生成的数组如下所示: array( 'Collection' => array( 'id' => '4fc923f5-0a58-453f-9507-0c0c86106d80', 'name'

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

我有两个模型-收集和项目 收藏有很多物品 属于收藏的物品

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

生成的数组如下所示:

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'
        )
    )
数组(
'集合'=>数组(
“id”=>“4fc923f5-0a58-453f-9507-0c0c86106d80”,
“名称”=>“
),
'Item'=>数组(
(int)0=>数组(
“id”=>“4fc92403-000c-4ed2-9dae-0c0c86106d80”,
“名称”=>“项目1”
),
(int)1=>数组(
“id”=>“4fc9241b-35ec-4810-b511-0c0c86106d80”,
“名称”=>“项目2”
),
(int)2=>数组(
“id”=>“4fc96e5d-ad74-4c05-a9da-0c0c86106d80”,
'名称'=>'项目3'
),
(int)3=>数组(
“id”=>“4fc96e64-a110-4036-bea2-0c0c86106d80”,
'名称'=>'项目4'
)
)
现在我可以从这个paginate调用中得到相同的结果,除了添加了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'
            )
      )
  )
);
$this->set('items',$this->paginate('Collection',array('Collection.id'=>$id));
排列(
(int)0=>数组(
'集合'=>数组(
“id”=>“4fc923f5-0a58-453f-9507-0c0c86106d80”,
“名称”=>“
),
'Item'=>数组(
(int)0=>数组(
“id”=>“4fc92403-000c-4ed2-9dae-0c0c86106d80”,
“名称”=>“项目1”
),
(int)1=>数组(
“id”=>“4fc9241b-35ec-4810-b511-0c0c86106d80”,
“名称”=>“项目2”
),
(int)2=>数组(
“id”=>“4fc96e5d-ad74-4c05-a9da-0c0c86106d80”,
'名称'=>'项目3'
),
(int)3=>数组(
“id”=>“4fc96e64-a110-4036-bea2-0c0c86106d80”,
'名称'=>'项目4'
)
)
)
);
我可以对返回的数组执行array_shift,但分页不起作用。我可以尝试为此创建自定义分页函数,但想知道是否有更简单的方法来使用我正在使用的标准分页,因为它有我想要的数据,只是额外的第一个数组父元素[0]

提前感谢您的建议。


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

好的,是否需要分页?您正在对结果进行分页,这通常会返回一个记录列表。在您的情况下,“列表”只包含一行(第0行)。但是,如果您的目的是对
项目
模型中的记录进行分页,而不是对
集合
模型中的记录进行分页,则应修改您的分页,并对
项目
模型进行分页。如果您需要相关帮助,请留下评论或使用链接更新您的问题感谢您的回复。我将从项目更新分页。我能够将“findType”=>“first”添加到集合中的paginate中,这使我获得了与“find”first相同的数组,但paginate仍然不在那里工作。我认为我尝试使用集合模型时遇到了这个错误,正如您所说,我应该从项目模型中执行此操作。再次感谢这是我的问题,但我也意识到我的foreach在我的观点被搞砸了。再次感谢你的提示,让我的头脑更清醒了一点。很高兴我能帮上忙。希望你能设法找到你想要的,否则就发布一个新问题。谢谢你的回答,是的,这就是我所做的。我试图发布我自己的答案,但stackover flow不允许我发布。$this->paginate=array('Item'=>array)('recursive'=>1,'limit'=>5,'conditions'=>array('Item.collection\u id'=>$id),'order'=>'Item.name asc')刚才调用的$this->set('items',$this->paginate('Item');啊,我只添加了数组重构,这样我就可以将其全部设置为一个变量了。)是的,很好。我没想到,但将来可能会派上用场。
<?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;

?>