Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cakephp 保存belongsTo模型_Cakephp - Fatal编程技术网

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 ....
                    )
    )
我会帮助你的。如何保存关联数据。您也可以使用这个方法

我希望它对你有用