表单辅助字符串 concat 将零转换为 cakephp 中的未设置

发布于 2024-09-06 07:26:42 字数 630 浏览 6 评论 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'ing它,单引号,双引号,双引号和{}都没有用。这是 PHP 的特性、CakePHP 的不鲁棒性还是我的愚蠢?

This has me beat. I'm trying to create an array of fields in cakePHP 1.2.5 & PHP 5.3.2
The array is zero based. On the first iteration, $count == 0. For some reason the string concatenation seems to convert this to null or unset which cake then interprets as "insert model name here", viz:

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">
...

I've tried casting the value, strval'ing it, single quotes, double quotes, double quotes and {} to no avail. Is this a PHP feature, a CakePHP unrobustness or me being dumb?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉目亦如画i 2024-09-13 07:26:42

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

I rebased the array at 1 and it works fine and as expected.

叹倦 2024-09-13 07:26:42

首先,一个约定是,如果您要保存映射到同一模型的字段并希望进行多个数据库插入,则数据数组的格式应如上面表示的 Mike 所示:

即 Model.{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][标题],Descriptor[title]

因此,如果您想使用帮助器,您应该按照它们预期的工作方式来使用它们。如果您发明了自己的边缘情况,而助手一开始并不支持这种情况,那么这并不是一个错误。

从你的例子来看 - 无论如何没有理由不使用 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';
        ...
        ...
    }
?>

First, it is a convention that if you are saving fields mapped to the same model and want multiple db inserts that the data array should be formatted as Mike expressed above:

i.e. Model.{n}.field

You can clearly see that they explicitly say that when you are saving the same fieldname in the same model multiple times that this is the convention as the title of the manual section is name "Field Naming Conventions"

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

You can't really call the input problem a CakePHP bug if the input method wasn't written to accommodate you using the method in an unintended fashion. The method explicitly splits the passed string on the "." character and assumes that if you are using a string with the "." character that you are intending to format your data array for saving using either Model->save or Model->saveAll

Secondly, when I test your code at my end it does exhibit a legit bug - it uses the numeric indexes I expect but duplicates them..

i.e. [0][0][Descriptor][title], 1[Descriptor][title]

When I move the index to where the save* functions expect it to be, the parsing is perfect.

i.e. [Descriptor][0][title], Descriptor[title]

So, if you want to use the helpers you should be using them in the ways they are intended to work. It isn't a bug if you invent your own edge case that wasn't intended to be supported by the helper to begin with.

Judging from your example - there is no reason not to use saveAll anyways. Do you have some reason for avoiding it; It seems to be the right way to do what you are asking.

** EDITED TO FIX TICKET http://cakephp.lighthouseapp.com/projects/42648/tickets/867 **

App this as 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)
        ));
    }
}
?>

Tell your controller to use the view with it's public $view property.

<?php
    class FooController extends Controller {
        ...
        ...
        var $view = 'App';
        ...
        ...
    }
?>
心凉 2024-09-13 07:26:42

您能否坚持 CakePHP 约定并将模型名称放在前面,然后是索引?

echo $form->input('NodeDescriptor.'.$count.'.title');

Can you stick to CakePHP conventions and put the model name first, followed by the index?

echo $form->input('NodeDescriptor.'.$count.'.title');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文