CakePHP saveAll不保存二级依赖项的密钥
我使用的是cake PHP2.x。我的数据库是这样设置的 评级有一个审查 评论中有很多照片CakePHP saveAll不保存二级依赖项的密钥,cakephp,Cakephp,我使用的是cake PHP2.x。我的数据库是这样设置的 评级有一个审查 评论中有很多照片 data => array( 'Rating' => array( 'rating' => '1', 'user_id' => '1', 'listing_id' => '55' ), 'Review' => array( 'title' => 'Good Servic
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 |
+----+---------+------------+-------------+-------+--------------------------------------+-----------+
在
查看中找到的照片中找到的查看中的有许多关系,而在中定义外键时,您没有完成关系
您的在Review.php
中有许多数组,应该如下所示:
public $hasMany = array(
'Photo' => array(
'className' => 'Photo',
'foreignKey' => 'review_uuid',
'conditions' =>'',
'order' => ''
)
);
注意foreignKey
参数。您告诉Cake,Review
有许多照片,每个照片记录都有一个名为Review\u uuid
的外键,用于标识它与评论的关系。您在照片
模型的belongsTo
数组中定义外键,但在查看
模型的hasMany
数组中没有定义外键,因此关系从未完成
您还应该定义与评分之间的关系以下
在Review.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' => ''
)
);
谢谢外键不见了。值得一提的是,它已经在我的模型中设置好了。为了避免一个冗长的问题,我只给出了部分标签。没问题,很高兴你找到了答案。