CakePHP有很多不能保存

时间:2012-03-21 06:31:20

标签: cakephp

我正在开发CakePHP 2.0,这是我的代码:

class Sheet extends AppModel{

    var $name = 'fc_sheets';
    var $hasMany = array(
        'Apspent' => array(
            'className' => 'Adspent',
            'foreignKey'=> 'sheetID',
        ),
    ); 
}

class Adspent extends AppModel{

    var $name = 'fc_adspents';
    var $hasOne = 'Sheet';
    var $belongsTo = 'Sheet';

}

在控制器中:

class SheetsController extends AppController{

    var $name = 'Sheets';

    function add(){
        $this->Sheet->save($this->data);
        //I have also tried this
        $this->Sheet->saveAll($this->data);
    }
}

以下是 print_r($this->data) 的调试:

Array ( 
    [Adspent] => Array ( [description] => Array ( [0] => Yellow Pages ) [price] => Array ( [0] => 200 ) ) 
    [Sheet] => Array ( [adFundConst] => 2 [warrFundConst] => 1 [pst] => 8 [gst] => 5 [hst] => 13 [adspent] => 200.00 [percentAdv] => Infinity [normalSales] => 0.00 [extraSales] => 0.00 [totalSales] => 0.00 [adFund] => 0.00 [warrFund] => 0.00 [royalty] => 200.00 [tax] => 26.00 [total] => 226.00 [matTotal] => 0.00 [totalDue] => 226.00 )
)

但是,Sheet数据是数据库中唯一保存的数据,而不是另一个。

任何人都知道我做错了什么?

3 个答案:

答案 0 :(得分:0)

'dependent'=> true 

在你有很多关系中添加这个

var $hasMany = array(
    'Apspent' => array(
        'className' => 'Adspent',
        'foreignKey'=> 'sheetID',
        'dependent'=> true, 
    ),
);

我会解决你的问题...

答案 1 :(得分:0)

可能是由于Adspent的descriptionprice字段中的数据是数组引起的。

你有这个:

Array ( 
    [Adspent] => Array (
        [description] => Array ( [0] => Yellow Pages )
        [price] => Array ( [0] => 200 ) 
    ) 
    [Sheet] => Array ( 
        [adFundConst] => 2
        [warrFundConst] => 1 
        [pst] => 8 
        [gst] => 5 
        [hst] => 13
        [adspent] => 200.00 
        [percentAdv] => Infinity 
        [normalSales] => 0.00 
        [extraSales] => 0.00
        [totalSales] => 0.00 
        [adFund] => 0.00 
        [warrFund] => 0.00 
        [royalty] => 200.00 
        [tax] => 26.00 
        [total] => 226.00 
        [matTotal] => 0.00 
        [totalDue] => 226.00
    )
)

尝试更改您的表单,以便您的表单在descriptionprice字段中发布没有数组的数据,如下所示:

Array ( 
    [Adspent] => Array (
        [description] => Yellow Pages
        [price] => 200 
    ) 
    [Sheet] => Array ( 
        [adFundConst] => 2
        [warrFundConst] => 1 
        [pst] => 8 
        [gst] => 5 
        [hst] => 13
        [adspent] => 200.00 
        [percentAdv] => Infinity 
        [normalSales] => 0.00 
        [extraSales] => 0.00
        [totalSales] => 0.00 
        [adFund] => 0.00 
        [warrFund] => 0.00 
        [royalty] => 200.00 
        [tax] => 26.00 
        [total] => 226.00 
        [matTotal] => 0.00 
        [totalDue] => 226.00
    )
)

然后保存,试试:

$this->Sheet->saveAssociated($this->request->data)

答案 2 :(得分:0)

正如您所提到的hasMany关系,那么您的数组结构应如下所示。

Array ( 
    [Adspent] => Array (
        [description] => Yellow Pages
        [price] => 200 
    ) 
    [Sheet] => 
[0] => Array ( 
        [adFundConst] => 2
        [warrFundConst] => 1 
        [pst] => 8 
        [gst] => 5 
)
[1] => Array ( 
        [adFundConst] => 2
        [warrFundConst] => 1 
        [pst] => 8 
        [gst] => 5 
    )
)

尝试以上述格式形成数组并查看。 有关详细信息,请参阅http://book.cakephp.org/1.3/en/view/1032/Saving-Related-Model-Data-hasOne-hasMany-belongsTo