CakePHP可容纳行为限制中间模型

时间:2013-10-10 17:15:14

标签: cakephp cakephp-2.3

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

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

2 个答案:

答案 0 :(得分:1)

尝试

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

答案 1 :(得分: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
                 )
...

如果这不是您想要的输出,请举例说明您的需求,我将编辑我的答案。 :)