代码之家  ›  专栏  ›  技术社区  ›  Leo

cakephp中将零转换为未设置的form helper字符串concat

  •  0
  • Leo  · 技术社区  · 15 年前

    数组是基于零的。在第一次迭代中,$count==0。由于某种原因,字符串串联似乎将其转换为null或unset,然后cake将其解释为“在此处插入模型名”,即:

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

    3 回复  |  直到 15 年前
        1
  •  1
  •   Leo    15 年前

        2
  •  1
  •   Abba Bryant    15 年前

    首先,如果要保存映射到同一模型的字段,并且需要多个db插入,则数据数组的格式应如上所述:

    i、 e.模型{n}.字段

    你可以清楚地看到他们 明确地 假设在同一模型中多次保存同一字段名时 这是惯例吗 因为手册的标题是“字段命名约定”

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

    如果输入方法的编写不是为了让您以非预期的方式使用该方法,那么您就不能真正地将输入问题称为CakePHP错误。该方法显式拆分“.”字符上传递的字符串,并假定如果使用的字符串带有“.”字符,则要格式化数据数组以使用任一模型保存->保存或建模->全部保存

    第二,当我在我的终端测试你的代码时,它确实显示了一个合法的错误-它使用了我期望的数字索引,但是复制了它们。。

    i、 e.[0][0][Descriptor][title], 1 [描述符][标题]

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

    i、 e.[描述符][0][标题], Descriptor [标题]

    所以,如果你想使用助手,你应该按照他们想要的方式来使用它们。如果您发明了自己的edge案例,而helper一开始并不支持它,那么它就不是一个bug。

    从你的例子来看,没有理由不使用saveAll。你有什么理由回避它吗;这似乎是做你所要求的事情的正确方法。

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

    <?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';
            ...
            ...
        }
    ?>
    
        3
  •  0
  •   Mike    15 年前

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

    echo $form->input('NodeDescriptor.'.$count.'.title');
    
    推荐文章