Insert multiple values into separate models


The story: let’s say I have a blog, and I have a posts table. For every post the user can attach multple files. So I created another files table for it (connected to the userrs table). So it’s two separate tables in database. Every post can have many attachement and all attachement belongs to one post. I set the foreign keys accordingly and generated the models.

I also managed to make the view so that with javascript if the user clicks on “add one more file” it’ll do what it needs. So the view is also fine, adding the 3rd file looks like this:

<tr id="file_sor-2"><td>

		<input id="ytFajl_filename_2" type="hidden" value="" name="Fajl[filename][2]"><input name="Fajl[filename][2]" id="Fajl_filename_2" type="file">		<div class="errorMessage" id="Fajl_filename_2_em_" style="display:none"></div>			</td><td>

		<input size="10" maxlength="50" name="Fajl[version][2]" id="Fajl_version_2" type="text">		<div class="errorMessage" id="Fajl_version_2_em_" style="display:none"></div>		</td><td>

		<select name="Fajl[platform_id][2]" id="Fajl_platform_id_2">

<option value="">--Kérlek válassz--</option>

<option value="1">Android 2.2</option>

<option value="3">Android 3.0</option>

</select>		<div class="errorMessage" id="Fajl_platform_id_2_em_" style="display:none"></div>		</td><td>

		<select name="Fajl[size_id][2]" id="Fajl_size_id_2">

<option value="">--Kérlek válassz--</option>

<option value="2">176x220</option>

<option value="1">240x320</option>

</select>		<div class="errorMessage" id="Fajl_size_id_2_em_" style="display:none"></div>		</td>


So in the PostController site I get something like this (content of $_POST[‘File’]) :

    [filename] => Array


            [0] => name1.jpg

            [1] => name2.jpg

            [2] => name3.jpg

            [3] => name4.jpg


    [version] => Array


            [0] => first

            [1] => second

            [2] => third

            [3] => fourth


    [platform_id] => Array


            [0] => 1

            [1] => 3

            [2] => 3

            [3] => 1


    [size_id] => Array


            [0] => 1

            [1] => 2

            [2] => 2

            [3] => 1


So basically every post has only one title, etc, which are coming from $_POST[‘Post’] but one post can have optional numbers of files which are coming from $_POST[‘File’].

In the uploadAction of controller I have these defined:

$model=new Post;

$model_file=new File;

I was not sure though how to save multiple $model_file’s, while only saving one Post $model. I also checked tabular input section of the doc, but I’m not sure that it’s for this kind of problem.

My question is: how could I save multiple Files model and only one post model at the same time?