0

当使用 cakephp Model->find() 方法时,我想以不同的方式对结果进行分组。

我的架构是:

  `id` int(11) NOT NULL AUTO_INCREMENT,
  `deviceid` varchar(255) NOT NULL,
  `user_id` int(11) NOT NULL,
  `sessionid` varchar(255) NOT NULL,
  `ip` text NOT NULL,
  `image_type` varchar(100) NOT NULL DEFAULT 'invalid_image',
  `filename` text NOT NULL,
  `analysis_basic` text COMMENT 'output from the image script',
  `analysis_admin` text,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `sessionid` (`sessionid`)

在 cakephp 我想得到这样的东西:

Array
(
    [0] => Array
    (
        [Picture] => Array
        (
            [sessionid] => somevalue
        )
        [0] => Array
        (
            [id] => 32
            [deviceid] => 5124125
            [userid] => 4541221
            ...and all other fileds
        )
        [1] => Array
        (
            [id] => 35
            [deviceid] => 5124125
            [userid] => 4541221
            ...and all other fileds
        )        
    )
    [1] => Array
    (
        [Picture] => Array
        (
            [sessionid] => someothervalue
        )
        [0] => Array
        (
            [id] => 38
            [deviceid] => 5124454
            [userid] => 34241
            ...and all other fileds
        )
        [1] => Array
        (
            [id] => 35
            [deviceid] => 5124454
            [userid] => 34241
            ...and all other fileds
        )    

cakephp 有可能吗?或者有没有更好的方法来实现类似于上面的东西?如果我能获得这样的结果,我知道 [0] 元素中的所有记录都具有相同的 sessionid .. 这就是原因。

4

1 回答 1

0

尝试:

$pictures = $this->Picture->find('all');
$grpPicts = array();

foreach($pictures as $p) {
   $sessid = $p['sessionid'];
   $grpPicts[$sessid]['Picture']['sessionid'] = $sessid;
   $grpPicts[$sessid][] = $p;
}

var_dump(array_values($grpPicts));

我知道这不是最优雅的解决方案,但它应该接近您正在寻找的解决方案。

于 2012-05-31T07:09:27.190 回答