How to edit a form with hasMany (multiple lines)

I have a Unit that has a lot of images and I am trying to display an edit form for those images on my edit page. As a result of entering the html form, the wrong model is displayed, so the correct value for this data is not populated. Here is my code:

<?php


echo $this->Form->create('Unit', array(
    'action' => 'edit',
    'inputDefaults' => array(
        'label' => false,
        'div' => false
        )
    )
);
echo $this->Form->hidden('id');

foreach($this->data['Image'] as $img) {

echo $this->Form->hidden('Image'.$img['id'].'id');

}
echo $this->Form->end('Update Photo');?>
<?php echo $this->Session->flash(); ?>

      

My html ends like this:

<form action="/units/edit/3" id="UnitEditForm" method="post" accept-charset="utf-8">
    <div style="display:none;">
       <input type="hidden" name="_method" value="POST"/>
    </div>
    <input type="hidden" name="data[Unit][id]" value="3" id="UnitId"/>
    <input type="hidden" name="data[Unit][Image1id]" id="UnitImage1id"/>
    <input type="hidden" name="data[Unit][Image2id]" id="UnitImage2id"/>
    <input type="hidden" name="data[Unit][Image3id]" id="UnitImage3id"/>
    <input type="hidden" name="data[Unit][Image4id]" id="UnitImage4id"/>
    <input type="hidden" name="data[Unit][Image5id]" id="UnitImage5id"/>
    <input type="hidden" name="data[Unit][Image6id]" id="UnitImage6id"/>
   <div class="submit">
       <input  type="submit" value="Update Photo"/>
   </div>
</form>

      

I've seen several tutorials, including the CakePHP tutorial, which shows you how you can enter "Model Name". [0] .field "etc, which is all good and useful when you know how many rows you have, but when does this happen in a CMS? I can't seem to find any information on how to create this form by going through all records into an array of available images.In the end, I want to have a hidden id field so that the record can be updated, show the actual image (and the ability to change it), this is the alt description (and the ability to change it), and I I suppose there is another hidden field for unit_id that binds it to this form.

UPDATE using

echo $this->Form->hidden('Image.'.$img['id'].'.id');

      

works now but returns this:

<input type="hidden" name="data[Image][1][id]" value="2" id="Image1Id"/>
<input type="hidden" name="data[Image][2][id]" value="3" id="Image2Id"/>
<input type="hidden" name="data[Image][3][id]" value="4" id="Image3Id"/>
<input type="hidden" name="data[Image][4][id]" value="5" id="Image4Id"/>
<input type="hidden" name="data[Image][5][id]" value="6" id="Image5Id"/>
<input type="hidden" name="data[Image][6][id]" id="Image6Id"/>

      

I need to start with [0], not [1]. Instead of getting the number by "id", I need to get the counted numbers in the $ img array, right? How to do it?

+3


source to share


2 answers


I got it working. Take a look at the below code. The key starts a counter that goes through each element of the array and uses the counter output to generate the correct number of inputs.

$count=0;
foreach($this->data['Image'] as $img) {
echo $this->Form->hidden('Image.'.$count.'.id');
$count++;
}

      

these outputs:



<input type="hidden" name="data[Unit][id]" value="3" id="UnitId"/>
<input type="hidden" name="data[Image][0][id]" value="1" id="Image0Id"/>
<input type="hidden" name="data[Image][1][id]" value="2" id="Image1Id"/>
<input type="hidden" name="data[Image][2][id]" value="3" id="Image2Id"/>
<input type="hidden" name="data[Image][3][id]" value="4" id="Image3Id"/>
<input type="hidden" name="data[Image][4][id]" value="5" id="Image4Id"/>
<input type="hidden" name="data[Image][5][id]" value="6" id="Image5Id"/>

      

You can see that the correct value is filled.

+1


source


This line is most likely causing problems:

    echo $this->Form->hidden('Image'.$img['id'].'id');

      



This will build the model.field as "Image1id", not what Cake expects "Image.1.id". Try rewriting it like this:

    echo $this->Form->hidden('Image.'.$img['id'].'.id');

      

+4


source







All Articles