Using CakePHP 1.3.6
I’m sure I may have to add code and examples, but I’ll start with this: I have a large form that is create()’d based on a parent model that ‘has many’ of several other models that have fields created in the form.
Several of the ‘child’ models (which are also marked as ‘belongs to’ the parent) have a ‘checked’ field (tinyint 1, checkbox) and a ‘notes’ field (text, textarea input).
From inside one of the child models, if I place debug($this->data); in any of the custom validation callbacks, I get this:
Array
(
[Preapp120aItem] => Array
(
[checked] => 1
[notes] => 1ere's some notes.
[agent_id] => 1
)
)
Where the textarea contains “here’s some notes.” and not “1ere’s…”, as above.
Also, upon doing a saveAll() on the parent, I receive this error, for the child model in question:
Notice (8): Array to string conversion [CORE/cake/libs/model/datasources/dbo_source.php, line 749]
Warning (512): SQL Error: 1054: Unknown column 'Array' in 'field list' [CORE/cake/libs/model/datasources/dbo_source.php, line 684]
Query: INSERT INTO `preapp120d_items` (`checked`, `notes`, `modified`, `created`) VALUES (1, Array, '2011-01-06 08:27:40', '2011-01-06 08:27:40')
Where part of the stack trace information shows this, about the model:
Preapp120dItem::$Preappform = Preappform object
$fields = array(
"checked",
"notes",
"modified",
"created"
)
$values = array(
"1",
array(
"preappform_id" => "18"
),
"2011-01-06 08:27:40",
"2011-01-06 08:27:40"
)
How or why is the notes field being populated with that array?
The parent model is called a ‘preappform’, and indeed, its ID is 18… but…?
Update 1:
As requested, here’s some code from the view.
Variable used later, placed before form:
$default_notes_element_options = array(
'type' => 'textarea',
'rows' => '2',
'class' => 'text'
);
Form Create:
<?php echo $form->create('Preappform', $default_form_create_options); ?>
<?php echo $form->input('Preappform.id', array('type'=>'hidden', 'value'=>$the_form['Preappform']['id'] )) ?>
Preapp120aItem fields
<td nowrap="nowrap"><?php echo $form->input('Preapp120aItem.checked', array('label'=>array('text' => 'Request this item', 'class' => 'checkbox-label'))) ?></td>
<td width="327">
<?php echo $form->input('Preapp120aItem.notes', $default_notes_element_options); ?>
<?php echo $form->input('Preapp120aItem.agent_id', array('type' => 'hidden', 'value' => '1' )); ?>
</td>
Update 2:
HTML as rendered, for the Preapp120aItem model.
<tr>
<td>120A</td>
<td nowrap="nowrap">
<input type="hidden" name="data[Preapp120aItem][checked]" id="Preapp120aItemChecked_" value="0" />
<input type="checkbox" name="data[Preapp120aItem][checked]" value="1" id="Preapp120aItemChecked" />
<label for="Preapp120aItemChecked" class="checkbox-label">Check this box</label>
</td>
<td width="327">
<textarea name="data[Preapp120aItem][notes]" rows="2" class="text" cols="30" id="Preapp120aItemNotes" ></textarea>
<input type="hidden" name="data[Preapp120aItem][agent_id]" value="1" id="Preapp120aItemAgentId" />
</td>
</tr>
My bad
I may not have made it clear enough that ‘Preappform’ was a parent model, that ‘hasMany’ ‘Preapp120aItem’ children.
Thus, in the form, despite the fact that I only intend to save one at a time, the View should have contained the following, in place of the similar code found in the original question:
The subtle difference being the “0” (zero) between Model name and Field name. I knew to do this when saving multiple of a related item, but not when saving one.
As for how/why Cake was somehow overwriting what ended up in the model’s
$this->datawith a “1” for the first position character: I have no stinkin’ idea, and I don’t intend to find out.