2013-05-28 24 views
1

我正在使用Cake 2.x.我的數據庫是這樣設置的。CakePHP saveAll不保存第二級別的密鑰

評級都有一個評論

評論有很多照片

data => array(
    'Rating' => array(
     'rating' => '1', 
     'user_id' => '1', 
     'listing_id' => '55' 
     ), 
    'Review' => array(
     'title' => 'Good Service', 
     'date_visited' => array(
      'month' => '05', 
      'day' => '28', 
      'year' => '2013', 
      ), 
     'service_used' => 'Easy Checkout', 
     'description' => 'After a fairly quick check-in, the check out service was also breeze ', 
     'Photo' => array(
      (int) 1 => array(
       'title' => 'Test', 
       'photo' => array(
       'name' => '2.JPG', 
       ), 
      'listing_id' => '55', 
      'user_id' => '1' 
      ) 
      ) 
     ) 
    ) 

Review.php

public $hasMany = array(
    'Photo' => array(
     'className' => 'Photo', 
     'conditions' =>'', 
     'order'  => '' 
     ) 
); 

Photo.php

public $belongsTo = array(
    'User' => array(
     'className' => 'User', 
     'foreignKey' => 'user_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ), 
    'Review' => array(
     'className' => 'Review', 
     'foreignKey' => 'review_uuid', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ), 
    'Listing' => array(
     'className' => 'Listing', 
     'foreignKey' => 'listing_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ) 
); 

終於RatingsController.php

$this->Rating->create(); 

if ($this->Rating->saveAll($this->request->data, array('deep' => true))) { 
    $this->Session->setFlash(__('The rating has been saved')); 
    $this->redirect(array('action' => 'index')); 
} 

問題是所有的數據正在保存,除了review_uuid(這也是同時創建))在照片模型。

mysql> select id,user_id,listing_id,review_uuid,title,photo,photo_dir from photos where ID=26; 
    +----+---------+------------+-------------+-------+--------------------------------------+----------- + 
    | id | user_id | listing_id | review_uuid | title | photo        | photo_dir | 
    +----+---------+------------+-------------+-------+--------------------------------------+----------- + 
    | 26 |  1 |   55 | NULL  | Test | 1a107372ef53ba26d7748a50c25e6b27.jpg | 01/77/74 | 
    +----+---------+------------+-------------+-------+--------------------------------------+-----------+ 

回答

2

它看起來並不像你定義在ReviewhasMany關係Photo發現外鍵,你不能完成的關係。

hasMany陣列Review.php應該是這樣的:

public $hasMany = array(
    'Photo' => array(
     'className' => 'Photo', 
     'foreignKey' => 'review_uuid', 
     'conditions' =>'', 
     'order'  => '' 
     ) 
); 

注意foreignKey參數。您告訴Cake,Review有很多照片,每張照片記錄都有一個名爲review_uuid的外鍵,用於標識它與評論的關係。您正在陣列中的Photo模型中定義外鍵,但Review模型中的hasMany陣列中未定義外鍵,因此關係從未完成。

你也應該定義ReviewRating

之間的關係belongsToReview.php

public $hasMany = array(
    'Photo' => array(
     'className' => 'Photo', 
     'foreignKey' => 'review_uuid', 
     'conditions' =>'', 
     'order'  => '' 
     ) 
); 

public $belongsTo = array(
    'Rating' => array(
     'className' => 'Rating', 
     'foreignKey' => 'rating_id', 
     'conditions' => '', 
     'order' => '' 
    ) 
); 
+0

感謝。 ForeignKey失蹤。當然,它已經在我的模型中建立了。我只給出了部分標籤,以避免很長的問題。 – ghitesh

+0

沒問題,很高興你知道了。 – Dan