2012-03-21 51 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