2013-06-22 48 views
0

我已經花了三天的時間來處理這個問題,但已經走到死衚衕,無法繼續。我知道如何使用非cakephp方法獲得所需的數據,但我更喜歡使用cakephp的模型關係。Cant'從cakephp獲取HABTM中的所有用戶數據

我的模型

class User extends AppModel { 
    var $hasOne = 'Profile'; 
    } 


class Profile extends AppModel { 
    var $belongsTo = 'User'; 
    var $hasAndBelongsToMany = array('Quad' => array('className' => 'Quad')); 
    } 


class Quad extends AppModel{ 
    var $belongsTo = 'User'; 
    var $hasAndBelongsToMany = array ('Performer' => array ('className' =>'Performer')); 
    } 


class Performer extends AppModel { 
    var $name = 'Performer'; 
    var $hasAndBelongsToMany = array('Quad' => array('className' => 'Quad')); 
    } 

MY HABTM TABLES

(perfomers:ID |名稱|圖像|創建|修改) (四邊形:ID | USER_ID |等.... ) (performers_quads:id | performer_id | quad_id)

MY Profiles控制器索引方法

public function index($uid) { 
    $user = $this->Profile->User->find('all', array(
           'contain' => array(
           'Quad' => array('contain'=>array('Performer'))), 
           'conditions' => array(
           'User.id' => $uid)));         

    $this->set(compact('user')); 
    } 

我的結果數組

UserArray ( 
      [0] => Array ( 
        [User] => Array ( 
           [id] => 19 
           [email] => *****@gmail.com 
           [password] => *** 
           [created] => 2013-06-13 23:35:42 
           [modified] => 2013-06-13 23:35:42) 

        [Profile] => Array ( 
            [id] => 21 
            [user_id] => 19 
            [username] => **** 
            [first_name] => **** 
            [last_name] => **** 
            [imageId] => ****.jpg 
            [created] => 2013-06-13 23:35:43 
            [modified] => 2013-06-17 01:05:22) 

        [Quads] => Array ( 
            [0] => Array ( 
              [id] => 65 [user_id] => 19 
              [title] => Test Event 
              [date] => 2013-06-15 16:53:00 
              [price] => 25.00 
              [venueID] => 1 
              [created] => 2013-06-17 01:05:22) 

            [1] => Array ( 
              [id] => 66 
              [user_id] => 19 
              [title] => Test Event 2 
              [date] => 2013-06-12 17:12:00 
              [price] => 50.00 
              [venueID] => 1 
              [created] => 2013-06-17 01:05:22)))) 

我需要

UserArray ( 
      [0] => Array ( 
        [User] => Array ( 
           [id] => 19 
           [email] => *****@gmail.com 
           [password] => *** 
           [created] => 2013-06-13 23:35:42 
           [modified] => 2013-06-13 23:35:42) 

        [Profile] => Array ( 
            [id] => 21 
            [user_id] => 19 
            [username] => **** 
            [first_name] => **** 
            [last_name] => **** 
            [imageId] => ****.jpg 
            [created] => 2013-06-13 23:35:43 
            [modified] => 2013-06-17 01:05:22) 

        [Quads] => Array ( 
            [0] => Array ( 
              [id] => 65 [user_id] => 19 
              [title] => Test Event 
              [date] => 2013-06-15 16:53:00 
              [price] => 25.00 
              [venueID] => 1 
              [created] => 2013-06-17 01:05:22 

              [Performer] => Array(
                  [0] => Array(
                    [id] => *** 
                    [name] => **** 
                    etc....))) 

            [1] => Array ( 
              [id] => 66 
              [user_id] => 19 
              [title] => Test Event 2 
              [date] => 2013-06-12 17:12:00 
              [price] => 50.00 
              [venueID] => 1 
              [created] => 2013-06-17 01:05:22 

              [Performer] => Array(
                  [0] => Array(
                    [id] => *** 
                    [name] => **** 
                    etc....))))))) 

我試圖做到的是獲得表演屬於屬於用戶的四邊形ProfilesController。任何幫助,將不勝感激。

回答

1

這是怎麼了,我終於結束瞭解決我的問題

型號:

class AppModel extends Model { 
var $actsAs = array(
'Containable');  
} 


class Performer extends AppModel { 
var $name = 'Performer'; 
} 


class Quad extends AppModel { 
var $belongsTo = 'User'; 
var $hasAndBelongsToMany = array ('Performer'); 
} 


class Profile extends AppModel { 
var $belongsTo = 'User'; 
} 


class User extends AppModel { 
var $hasOne = 'Profile'; 
var $hasMany = 'Quad'; 
} 

在配置文件控制器:

public function index($uid) 
{ 
    $user = $this->Profile->User->find('all', array(
        'contain' => array('Quad' =>array('Performer'), 'Profile'), 
        'conditions' => array('User.id' => $uid))); 

    $this->set(compact('user')); 
} 

並將結果:

UserArray 
(
    [0] = Array 
    (
     [User] = Array 
      (
       [id] = 19 
       [email] = *****@gmail.com 
       [password] ***** 
       [created] = 2013-06-13 23:35:42 
       [modified] = 2013-06-13 23:35:42 
      ) 

     [Profile] = Array 
      (
       [id] = 21 
       [user_id] = 19 
       [username] = ******** 
       [first_name] = ********* 
       [last_name] = ********** 
       [imageId] = id8fkwd_b81a24524aa20f2c4156277d1b6a7c27_1371445522.jpg 
       [created] = 2013-06-13 23:35:43 
       [modified] = 2013-06-17 01:05:22 
      ) 

     [Quad] = Array 
      (
       [0] = Array 
        (
         [id] = 65 
         [user_id] = 19 
         [title] = Test Event 
         [date] = 2013-06-15 16:53:00 
         [price] = 25.00 
         [venueID] = 1 
         [Performer] = Array 
          (
           [0] = Array 
            (
             [id] = 1 
             [name] = ******** 
             [verified] = 1 
             [photo] = 1 
             [photoname] = *****1370810034.jpg 
            ) 

           [1] = Array 
            (
             [id] = 2 
             [name] = ****** 
             [verified] = 1 
             [photo] = 1 
             [photoname] = ******_1370810315.jpg 
            ) 

          ) 

         [created] = 2013-06-15 16:53:00 
        ) 

       [1] = Array 
        (
         [id] = 66 
         [user_id] = 19 
         [title] = Test Event 2 
         [date] = 2013-06-12 17:12:00 
         [price] = 50.00 
         [venueID] = 1 
         [Performer] = Array 
          (
           [0] = Array 
            (
             [id] = 1 
             [name] = ******* 
             [verified] = 1; 
             [photo] = 1 
             [photoname] = *****_1370810034.jpg 
            ) 

           [1] = Array 
            (
             [id] = 2 
             [name] = ****** 
             [verified] = 1 
             [photo] = 1 
             [photoname] = ******_1370810315.jpg 
            ) 

           [2] = Array 
            (
             [id] = 3 
             [name] = ******** 
             [verified] = 1 
             [photo] = 1 
             [photoname] = *****_1370812455.jpg 
            ) 

          ) 

         [created] = 2013-06-15 16:53:00 
        ) 

      ) 

    ) 

希望這有助於其他人通過CakePHP的HABTM隧道去....