Cakephp 保存belongsTo模型
我有两个模型:照片和评分。每张Cakephp 保存belongsTo模型,cakephp,Cakephp,我有两个模型:照片和评分。每张照片,都应该有一个分数。因此,我将照片设置为属于分数: class Photo extends AppModel { public $actsAs = array('Containable'); public $belongsTo = array('Score'); } scores表有一个photo\u id列。(用于其照片id) 但是,有一个问题。。。。如何保存照片和分数?正如您在问题中所问:“每张照片,都应该有一个分数”。所以你必须改变你的照
照片
,都应该有一个分数
。因此,我将照片
设置为属于分数
:
class Photo extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array('Score');
}
scores
表有一个photo\u id
列。(用于其照片id)
但是,有一个问题。。。。如何保存照片
和分数
?正如您在问题中所问:“每张照片
,都应该有一个分数
”。所以你必须改变你的照片模型。这将看起来像:
class Photo extends AppModel {
public $actsAs = array('Containable');
public $hasOne = array('Score');
}
Array(
[Photo] => Array(
[img_name] = abc.jpg
[image_group] = cricket
... other fields ...
)
[Score] => Array(
[score_gain] => 10
... other fields ....
)
)
您必须使用该模型来调用saveAll()方法,该方法需要先保存。这样,其他关联模型可以获得新插入的记录的值,并将其视为外键
$this->Photo->saveAll($this->request->data, array('deep' => true));
您的请求数组($this->request->data
)应该如下所示:
class Photo extends AppModel {
public $actsAs = array('Containable');
public $hasOne = array('Score');
}
Array(
[Photo] => Array(
[img_name] = abc.jpg
[image_group] = cricket
... other fields ...
)
[Score] => Array(
[score_gain] => 10
... other fields ....
)
)
我会帮助你的。如何保存关联数据。您也可以使用这个方法
我希望它对你有用