0

这让我打败了。我正在尝试在 cakePHP 1.2.5 和 PHP 5.3.2 中创建一个字段数组,该数组从零开始。在第一次迭代中,$count == 0。由于某种原因,字符串连接似乎将其转换为 null 或取消设置,然后蛋糕将解释为“在此处插入模型名称”,即:

for($count=0;$count<$num;$count++)
{
   echo $form->input($count.'.NodeDescriptor.title');
}

<input name="data[NodeDescriptor][NodeDescriptor][title]" type="text" id="NodeDescriptorNodeDescriptorTitle" />
<input name="data[1][NodeDescriptor][title]" type="text" id="1NodeDescriptorTitle" /></div><tr><td><div class="input select">
...

我试过强制转换值,对其进行strval,单引号,双引号,双引号和{}无济于事。这是 PHP 特性、CakePHP 的不鲁棒性还是我很笨?

4

3 回答 3

1

我将数组重新设置为 1,它可以正常工作并且符合预期。

于 2010-06-23T12:11:55.013 回答
1

首先,如果您要保存映射到同一模型的字段并想要多个数据库插入,则数据数组的格式应为 Mike 上面表示的格式:

即模型。{n}.field

您可以清楚地看到,他们明确表示,当您在同一模型中多次保存相同的字段名时,这是约定,因为手册部分的标题是名称“字段命名约定”

http://book.cakephp.org/view/1390/Automagic-Form-Elements#Field-naming-convention-1391

如果输入法不是为了让您以非预期的方式使用该方法而编写的,那么您不能真正将输入问题称为 CakePHP 错误。该方法在“。”上显式拆分传递的字符串。字符并假设如果您使用带有“。”的字符串。您打算格式化数据数组以使用 Model->save 或 Model->saveAll 保存的字符

其次,当我在最后测试您的代码时,它确实表现出一个合法的错误——它使用了我期望的数字索引,但重复了它们..

即 [0][0][Descriptor][title], 1 [Descriptor][title]

当我将索引移动到 save* 函数期望的位置时,解析是完美的。

即[描述符][0][标题],描述符[标题]

因此,如果您想使用助手,您应该按照它们的预期工作方式使用它们。如果您发明了自己的边缘案例,而该案例一开始并不打算由助手支持,这不是错误。

从您的示例来看-无论如何都没有理由不使用 saveAll 。你有什么避免它的理由吗?这似乎是做你所要求的正确方法。

** 编辑修复票http://cakephp.lighthouseapp.com/projects/42648/tickets/867 **

将此应用为 app/views/app_view.php

<?php

App::import('View', 'View', false);

class AppView extends View {

    /**
     * Constructor
     *
     * @param object $controller
     */
    function __construct(&$controller){
            parent::__construct($controller);
    }

    /**
     * Temporary View::entity fix for 1.2.5
     * Returns the entity reference of the current context as an array of identity parts
     *
     * @return array An array containing the identity elements of an entity
     * @access public
     */
    function entity() {
        $assoc = ($this->association) ? $this->association : $this->model;
        if (!empty($this->entityPath)) {
            $path = explode('.', $this->entityPath);
            $count = count($path);
            if (
                ($count == 1 && !empty($this->association)) ||
                ($count == 1 && $this->model != $this->entityPath) ||
                ($count == 2 && !empty($this->fieldSuffix)) ||
                is_numeric($path[0]) && !empty($assoc)
            ) {
                array_unshift($path, $assoc);
            }
            return Set::filter($path);
        }
        return array_values(Set::filter(
            array($assoc, $this->modelId, $this->field, $this->fieldSuffix)
        ));
    }
}
?>

告诉您的控制器使用具有公共 $view 属性的视图。

<?php
    class FooController extends Controller {
        ...
        ...
        var $view = 'App';
        ...
        ...
    }
?>
于 2010-06-18T17:18:40.387 回答
0

你能坚持 CakePHP 的约定,把模型名放在第一位,然后是索引吗?

echo $form->input('NodeDescriptor.'.$count.'.title');
于 2010-06-18T14:34:26.063 回答