2012-06-29 42 views
1

我很努力地找到答案。我只用了一個月的CakePHP,並且遇到了問題。這是我可以通過手動插入值來解決的問題,但我希望我的數據能夠預先填充。這裏是正在發生的事情:

的型號是產品,它的hasMany「Dynamicprice」

我用7(/產品/編輯/ 7)的ID測試產品。

我的編輯功能的第一部分是:

public function edit($id = null) { 
    $this->Product->id = $id; 

    if (!$this->Product->exists()) { 
     throw new NotFoundException('Invalid Product'); 
    } 

    if ($this->request->is('get')){ 
     $this->request->data = $this->Product->read(); 
    } 
    debug($this->request->data); 
    //other stuff setting vars for drop-down lists 
} 

debug($this->request->data); 

給了我下面的:

array(
'Product' => array(
'id' => '7', 
'category_id' => '70', 
'name' => 'Full Test', 
'description' => 'This is to test all features', 
'price' => '0.00', 
'aesthetic' => true, 
'image' => '', 
'price_structure' => '2', 
'suggest_for' => '', 
'created' => '2012-06-28 12:49:06', 
'modified' => '2012-06-28 12:49:06' 
), 
'Dynamicprice' => array(
    (int) 0 => array(
    'id' => '15', 
    'product_id' => '7', 
    'drop' => '600', 
    'prices' => '6000:9.99, 12000:18.99 ' 
), 
    (int) 1 => array(
    'id' => '16', 
    'product_id' => '7', 
    'drop' => '1200', 
    'prices' => '6000:19.99, 12000:28.99 ' 
), 
    (int) 2 => array(
    'id' => '17', 
    'product_id' => '7', 
    'drop' => '2400', 
    'prices' => '6000:29.99, 12000:38.99 ' 
) 
) 
) 

然而,儘管一切都在[ '產品']預先填充['Dynamicprice']數組未預先​​填充以下內容:

<?php 
echo $this->Form->input('Dynamicprices.0.id'); 
echo $this->Form->input('Dynamicprices.0.drop', array('label' => 'Drop 1 (mm). Enter "0" for "Any Drop"')); 
echo $this->Form->input('Dynamicprices.0.prices', array('label' => 'Prices 1', 'type' => 'textarea', 'rel' => 'dynamic')); 
?><hr><?php 
echo $this->Form->input('Dynamicprices.1.id'); 
echo $this->Form->input('Dynamicprices.1.drop', array('label' => 'Drop 2 (mm).')); 
echo $this->Form->input('Dynamicprices.1.prices', array('label' => 'Prices 2', 'type' => 'textarea', 'rel' => 'dynamic')); 
?><hr><?php 
echo $this->Form->input('Dynamicprices.1.id'); 
echo $this->Form->input('Dynamicprices.2.drop', array('label' => 'Drop 3 (mm).')); 
echo $this->Form->input('Dynamicprices.2.prices', array('label' => 'Prices 3', 'type' => 'textarea', 'rel' => 'dynamic')); 
?> 

我是否期待他們自動填充,如果有的話,我做了什麼錯了?

我創建/Model/Dynamicprice.php與以下,以確保公正:

class Dynamicprice extends AppModel { 
    public $name = 'Dynamicprice'; 
    public $belongsTo = 'Product'; 

但正如我預期沒有任何改變。

回答

1

這是本週我第二次做這個;問一個愚蠢的問題。是的,我很期待他們預先填充。問題是我的命名習慣混淆了。 「Dynamicprices.1.drop」最後不應該有's'。傻我!