2012-03-21 77 views
0

我的工作CakePHP的2.0,这里是我的代码:的CakePHP的hasMany不节能

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) 
) 

但是工作表数据是唯一一个保存在数据库中的数据 另一个。

任何人有一个想法,我做错了什么?

回答

0
'dependent'=> true 

在有很多关系

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

我来解决你的问题添加此...

+0

感谢,但仍无法正常工作 – 2012-03-21 07:28:34

+0

$ ACTAS =“表” ...加入这一行Adspent类...可能是帮助 – 2012-03-21 07:37:14

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) 
+0

仍然没有工作,我不知道我错过了什么 – 2012-03-21 07:33:06

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