0

CakePHP 我正在使用可包含的行为,但我怀疑是否要停止来自中间模型的数据。

假设我有这个结构

Song -> Tracklist -> Album

所以当我把Containable行为放在Song Model它上面时,会用所需的字段取出专辑数据。但在每次调用中,它也会获取中间模型 Tracklist 的全部数据。

我的可包含行为是这样的。

$contain =  array('Tracklist'=>array('Album'=>array('id','name')));
$this-Song->find('first', array('contain'=>$contain));

像这样。我的数据就像

 [Tracklist] => Array
    (
        [0] => Array
            (
                [id] => 50
                [album_id] => 229
                [song_id] => 38
                [video_link] => 
                [status] => 0
                [featured] => 0
                [user_id] => 
                [created] => 
                [modified_by] => 
                [modified] => 
                [Album] => Array
                    (
                        [name] => Marasim
                        [slug] => marasim
                    )
            )

        [1] => Array
            (
                [id] => 2748
                [album_id] => 489
                [song_id] => 38
                [video_link] => 
                [status] => 1
                [featured] => 0
                [user_id] => 
                [created] => 2013-10-09 17:58:53
                [modified_by] => 
                [modified] => 2013-10-09 17:58:53
                [Album] => Array
                    (
                        [name] => Dummy Album
                        [slug] => dummy-album
                    )

                    )
            )
        [2] => Array
            (
                [id] => 2749
                [album_id] => 489
                [song_id] => 38
                [video_link] => 
                [status] => 1
                [featured] => 0
                [user_id] => 
                [created] => 2013-10-09 17:59:42
                [modified_by] => 
                [modified] => 2013-10-09 17:59:42
                [Album] => Array
                    (
                        [name] => Dummy Album
                        [slug] => dummy-album
                    )
            )
     )

我只想限制来自中间模型的数据。那可能吗 ?

4

2 回答 2

1

尝试

  $contain =  array('Tracklist'=>array(
        'fields' = array('id'),
        'Album'=>array('id','name')
  ));
  $this-Song->find('first', array('contain'=>$contain));
于 2013-10-11T05:11:01.283 回答
0
$data = $this-Song->find('first', array('contain'=>$contain));

// Extract the Album's of the playlist
$data = Hash::extract($data, '{n}.Tracklist.Album');

// debug($data)
[Album] => Array (
                    [name] => Marasim
                    [slug] => marasim
                 )
...

如果这不完全是您想要的输出,请给我一个您想要的示例,我将编辑我的答案。:)

于 2013-10-11T10:01:05.773 回答