2016-08-02 46 views
0

我有這樣的代碼模型用戶:如何在CakePHP中創建中可容納的行爲在虛擬場2.6

$hasOne = array(
    'Member' => array(
     'className' => 'Member', 
     'foreignKey' => 'user_id', 
     'dependent' => true 
     ) 
    ); 


function rest_findUserById($id = NULL) { 
    $row = $this->find(
     'first', 
     array(
      'contain' => array(
       'UserSession'=>array(
        'fields'=>array('user_id', 'session_token') 
       ) , 
       'Member' => array(
        'fields' => array("*") 
       ) 
      ), 
      'fields' => array('username', 'email'), 
      'conditions' => array('User.id' => $id) 
     )); 

     if (!$row) { 
      return FALSE; 
     } 
     debug($row);exit; 
} 

,並在模型成員:

$virtualFields = array(
      'full_name' => 'CONCAT(Member.first_name, " ",Member.last_name)' 
     ); 

    $belongsTo = array(
     'Country' => array(
      'className' => 'Country', 
      'foreignKey' => 'country_id', 
      'conditions' => '', 
      'fields' => '', 
      'order' => '' 
     ), 


      'User' => array(
      'className' => 'User', 
      'foreignKey' => 'user_id', 
      'conditions' => '', 
      'fields' => '', 
      'order' => '' 
     ) 
    ); 
?> 

當我打電話機能的研究rest_findUserById()我得到這個輸出:

/app/Model/User.php (line 378) 

array(
    'User' => array(
     'username' => 'testuser', 
     'email' => '[email protected]', 
     'id' => '71' 
    ), 
    'Member' => array(
     'id' => '11', 
     'user_id' => '71', 
     'first_name' => 'Whjc', 
     'last_name' => 'test_user_lname', 
     'email' => '', 
     'phone' => '778899554455', 
     'image_dir' => '11', 
     'image' => '92e20fd0260dcc5ea289611221723b6a.jpg', 
     'address_line_1' => 'Shhd', 
     'address_line_2' => 'sdf', 
     'city' => 'Los Angeles Area', 
     'state' => 'delhi', 
     'country_id' => '0', 
     'zip_code' => '56456', 
     'is_address_different' => false, 
     'date_of_birth' => '0000-00-00', 
     'referred_by' => 'asdf', 
     'created' => '2016-07-27 13:52:30', 
     'created_by' => '3', 
     'modified' => '2016-08-02 12:25:22', 
     'modified_by' => '3', 
     'status' => false 
    ), 
    'UserSession' => array(
     (int) 0 => array(
      'user_id' => '71', 
      'session_token' => 'a685b3db5ab87a928716c7d8b3272572' 
     ) 
    ) 
) 

但是當我調用這個代碼:

$this->Member->recursive = -1; 
     debug($this->Member->find('first')); 

我得到這樣的輸出:

/app/Model/User.php(線377)

array(
    'Member' => array(
     'id' => '4', 
     'user_id' => '64', 
     'first_name' => 'SDFS SDF', 
     'last_name' => 'test_user_lname', 
     'email' => '', 
     'phone' => '778899554455', 
     'image_dir' => '', 
     'image' => '', 
     'address_line_1' => 'sdf', 
     'address_line_2' => 'sdf', 
     'city' => 'Los Angeles Area', 
     'state' => 'delhi', 
     'country_id' => '0', 
     'zip_code' => '56456', 
     'is_address_different' => false, 
     'date_of_birth' => '1970-01-01', 
     'referred_by' => 'asdf', 
     'created' => '2016-07-22 15:25:30', 
     'created_by' => '0', 
     'modified' => '2016-07-22 15:25:30', 
     'modified_by' => '0', 
     'status' => false, 
     'full_name' => 'SDFS SDF test_user_lname' 
    ) 
) 

,你可以看到虛擬領域ie'full_name」在即將到來第二個輸出,但不是第一個輸出。 我該如何解決這個問題?

回答

1

manual

你不能在關聯模型

所提出的解決方案是將虛擬字段複製到模型中使用virtualFields,這樣

/應用/型號/User.php

$this->virtualFields['member_full_name'] = $this->Member->virtualFields['full_name']; 

當然,這對hasMany或HABTM關係不起作用。還請注意,通過這種方式,您可以在User數據陣列中找到虛擬字段,而不是在Member數據陣列中找到虛擬字段