1

我正在努力寻找这个问题的答案。我只使用 CakePHP 一个月就遇到了问题。这是我可以通过手动插入值来解决的问题,但我希望我的数据能够预先填充。这是正在发生的事情:

该模型是具有许多“动态价格”的产品

我正在测试一个 id 为 7 (/products/edit/7) 的产品。

我的编辑功能的第一部分是:

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

但是,虽然 ['Product'] 中的所有内容都预先填充了 ['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';

但正如我所料,它并没有改变任何东西。

4

1 回答 1

1

这是我本周第二次这样做;问一个愚蠢的问题。是的,我期望他们预先填充是正确的。问题是我把我的命名约定搞混了。“Dynamicprices.1.drop”最后不应该有“s”。傻我!

于 2012-06-29T11:24:16.133 回答