title: Lithium PHP, MongoDB and form helpers for arrays
body: <p>I'm trying to create an edit form using Lithium to edit some MongoDB data. My data (produced by another tool) looks like this:</p>
  
  <pre><code>{
    "thing_a" : "value_a",
    "thing_b" : "value_b",
    "settings" : 
    {
      "sub_thing_a" : ["sub_value_a", "sub_value_b"]
    }
  }
  </code></pre>
  
  <p>The problem I'm having is with the array 'sub_thing_a' in 'settings'. I need to display a text box for each value so I can edit them and save them back. The ultimate aim here is to use some jQuery to add/delete text boxes to/from the form and then values from the array - but for now I'm trying to just get a simple version working that will let me edit the values and save them away.</p>
  
  <p>My model is really simple:</p>
  
  <pre><code>&lt;?php
  namespace app\models;
  
  class Test extends \lithium\data\Model {
      protected $_meta = array('source' =&gt; 'test');
  }
  ?&gt; 
  </code></pre>
  
  <p>and the controller likewise:</p>
  
  <pre><code>&lt;?php
  namespace app\controllers;
  
  use app\models\Test;
  
  class TestsController extends \lithium\action\Controller {
    public function index() {
      $tests = Test::all();
      return compact('tests');
    }
  
    public function edit($id=null) {  
      if(isset($id)) {
        $test = Test::find($id);
      } else {
        $test = Test::create();
      }
  
      if ($this-&gt;request-&gt;data) {
        if ($test-&gt;save($this-&gt;request-&gt;data)) {
          $this-&gt;redirect('/tests/index');
        }
      }
  
      return compact('test');              
    } 
  }
  ?&gt;
  </code></pre>
  
  <p>Problems start with the edit form - As I have it now, it will display the values of my array, but the data does not get written correctly. Any clues as to how I should approach this? (Note: As I mentioned earlier, I will need to produce a dynamic version of this that allows me to add/delete text boxes to/from the form, so I do need to be able to take some sort of control of the helper - in case there is some really easy 'convention' way of doing this.)</p>
  
  <p>edit.html.php:</p>
  
  <pre><code>&lt;?=$this-&gt;form-&gt;create($test); ?&gt;
  &lt;?=$this-&gt;form-&gt;field('thing_a'); ?&gt;
  
  &lt;?php foreach ($test-&gt;settings-&gt;sub_thing_a as $i=&gt;$elem): ?&gt;
  
  &lt;?=$this-&gt;form-&gt;field('settings.sub_thing_a',array('label'=&gt;'thing', 'value'=&gt;$test-&gt;settings-&gt;sub_thing_a[$i]));?&gt;
  
  &lt;?php endforeach; ?&gt;
  
  &lt;?=$this-&gt;form-&gt;submit('save'); ?&gt;
  &lt;?=$this-&gt;form-&gt;end(); ?&gt;
  </code></pre>
  
  <p>and index.html.php (for completeness)</p>
  
  <pre><code>&lt;?php foreach($tests as $test): ?&gt;
  
  &lt;h2&gt;&lt;?=$this-&gt;html-&gt;link($test-&gt;thing_a,'/tests/edit/'.$test-&gt;_id); ?&gt;&lt;/h2&gt;
  
      &lt;?php foreach($test-&gt;settings-&gt;sub_thing_a as $item): ?&gt;
  
  &lt;h4&gt;&lt;?=$item ?&gt;&lt;/h4&gt;
  
  &lt;?php endforeach; ?&gt;
  
  &lt;?php endforeach; ?&gt;
  </code></pre>