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 |
+----+---------+------------+-------------+-------+--------------------------------------+-----------+
感謝。 ForeignKey失蹤。當然,它已經在我的模型中建立了。我只給出了部分標籤,以避免很長的問題。 – ghitesh
沒問題,很高興你知道了。 – Dan