开发者

form helper string concat converting zero to unset in 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 thi开发者_高级运维s a PHP feature, a CakePHP unrobustness or me being dumb?


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


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';
        ...
        ...
    }
?>


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜