The examples on this site are currently tested to work on Phalcon V3.4 and Phalcon Devtools V3.2 Some issues may arise when using later versions.

Please get in touch or post a comment below the post if you encounter a problem.
If an entity (a person or an item) needs to have multiple images for each item then you will need an extra database entity and model to store those images. In the following example we imagine a situation where each member of the tennis club can upload multiple images of themselves. Firstly we need a new table in the database in which to store the images.

Create table memberImage(
    id int auto_increment,
    memberid int,
    description varchar(30),
    imagefile longblob,
    primary key(id),
    foreign key(memberid) references member(id)
);

Next we need to scaffold the memberImage table.

phalcon scaffold memberImage --get-set --ns-models=tennisClub

To get the member table to pick up on the one-to-many relationship between the member and the memberImage we need to re-generate the Member model class using --force.

Phalcon model member --force --get-set --namespace=tennisClub

This will re-generate new aspects of the model but will keep any customisations or validations you have created.

Next modify the newAction() function of the MemberImageController as follows so that it takes a single parameter containing the member's id. Any images uploaded will be associated with this id.

public function newAction($memberid)
{
	$this->view->memberid = $memberid;
}

Next you need to modify the app/views/memberimage/new.phtml view to allow the user to upload multiple images.

First add the following setting to the form tag

"enctype" => "multipart/form-data",

Because the memberid is being passed to the form rather than entered by the user we can replace the <div> which contains the memberid field with a simple a hidden input field which will contain the memberid so that this information will be passed when the form is submitted and we can associate these images with the appropriate member.

<input type="hidden" name="memberid" value="<?php echo $memberid?>"/>

Now modify the <div> containing the description field as follows.

<div class="form-group">
    <label for="fieldDescription" class="col-sm-2 control-label">Description</label>
    <div class="col-sm-10">
        <?php echo $this->tag->textField(["description[]", "size" => 30, "class" => "form-control", "id" => "fieldDescription[]"]) ?>
    </div>
</div>

Notice the [ ] brackets in the name of the field. This will allow multiple fields with the same name to be uploaded as an array. In this way you can copy and paste this div tags as many times as you want on the same form. Three images should be enough for each member of my tennisClub

Do the same for the memberImage field using the following <div> tag.

<div class="form-group">
    <label for="fieldImageFile" class="col-sm-2 control-label">ImageFile</label>
    <div class="col-sm-10">
        <?php echo $this->tag->fileField(["imageFile[]", "size" => 30, "class" => "form-control", "id" => "fieldImageFile[]"]) ?>
    </div>
</div>

Now when you visit http://localhost/tennisClub/memberImage/new/5 -your form should look something like the one below. 

Notice the memberid being passed in the url. In a typical website it is more likely that the memberid would be picked up from session data. Members would probably be required to login to the system to upload their own photos. Once a user is logged useful information such as their member id will be stored in the session. See the security section for an explanation of how to do this.

Alternatively, we could add a link on the member/search along with the Edit and Delete links which exists for each member we would have an Add Images link. The following lines in the app/views/member/search.phtml file create the Edit and Delete links


Add the following line just below them to create a Add Images link.

<td><?php echo $this->tag->linkTo(["memberimage/new/" . $member->getId(), "Add Images"]); ?></td>

Next we'll modify the createAction() in the MemberimageController to allow us to process a form with multiple image files attached.