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.

The tennis club application should ideally allow members to book a court for a particular time of the day. The booking table resolves the many to many relationship between the member and the court. Each member may book many courts(over a period of time) and each court may be booked by many members.

To create CRUD screens/actions we scaffold the Court table of the database

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

and the Booking table

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

Now if you visit http://localhost/tennisClub/booking/new you will see the following screen

To make this screen a little bit more usable we ideally should have a date picker on the date, time pickers on the start time and end time and dropdown lists for the memberid and courtid to allow the user select from a list of possible members and possible courts.

Lets start with adding the dropdown lists. To make a list of members and courts we need to have an array of member objects and an array of court objects available to the view /app/booking/new.phtml. The best place to retrieve that information from the database is in the newAction() function in the Booking controller. Edit the file /app/controllers/BookingController.php and modify the newAction function as follows

public function newAction()
{
	$this->view->members = tennisClub\Member::find();
	$this->view->courts = tennisClub\Court::find();
}
$this->view->members is a new view variable which will be available within the view associated with this action (new.phtml) as  a variable called simply $members. Member::find() is a function which will return all the members as an array full of Member objects.Similarly $courts will be available as a variable containing an array of Court objects. In this way we can effectively pass variables to the view from the controller.
Before we modify the view to include the dropdowns we can make our task a little bit easier by modifying the Member and Court Model classes to include __toString() magic methods. A __toString() magic method determines how an object reacts when it is treated as a string. Modify the file /app/models/Member.php to include the following function:

public function __toString()
{
	return $this->firstname . " " . $this->surname;
}
This will ensure that the Member's fullname is displayed whenever the object is treated as a String. Now modify the file /app/models/Court.php to include the following funciton:
public function __toString()
{
	return "Court: " . $this->id . ", Surface: " . $this->surface;
}
This will ensure the Court's id and surface type are returned when the object is treated as a String.
Next we need to modify the view to include the dropdown lists for Member and Court. Edit the file /app/views/booking/new.phtml.  The two <div> blocks blow contain code to create the label and textfield for memberid and courtid. The blocks outlined in red use phalcon's tag helpers to create the appropriate html tags. Some programmers unfamiliar with html and PHP may find tag helpers useful. For me they sometimes add an extra layer of unnecessary complexity. 


Remove the code outlined in red and replace it with the code snippets below. As always take care to preserve the indentation in the code. For the memberid use the follwing:
<select name="memberid" id="fieldMemberid" class="form-control">
    <?php foreach ($members as $member) { echo "<option value='" . $member->getID() . "'>$member</option>"; }?>
</select>

For the courtid field use the following code snippet:

<select name="courtid" id="fieldCourtid" class="form-control">
    <?php foreach ($courts as $court) { echo "<option value='" . $court->getID() . "'>$court</option>"; }?>
</select>

Now when you visit the new booking form you will see a dropdown list for both members and courts each of which has been populated from the member table and court table in the database.


The $members and $courts variables are arrays of objects which was set in the newAction() function of the bookings controller. The loop creates an <option></option> tag for each element of the array - i.e. for each member of the club. Importantly the value is set as the members id. This means that, although the user selects from a list of names, what gets passed when the form is submitted is actually the memberid. Although the look of the form has changed, this allows it to function in the exact same way as before and no back-end code needs to be changed to handle the submitted data.

Between the <option></option> tags we need only put $member and $court. In this way we are treating the object as a string. The __toString() magic method will be called in each case and what will be displayed there is whatever is returned by the __toString() function.

The class="form-control" attribute in the <select> tag ensures that the dropdown is bootstrap ready and will fit in with the rest of the form.

To finish tidying up the form we want to add a date-picker and time-pickers to the date and time fields. In this case we will use the tag helper as there are tag helpers for date and time fields which make the modification very simple. Simply change the word textField to dateField or timeField as necessary to achieve the desired result as follows:


The fee should ideally be automatically calculated by a business rule rather than input by the user. There are a range of different ways to approach the business rule depending on it's complexity. For example a court might most €5 per our for Senior members and €2 per hour for junior members. For simplicity sake at this point we can remove the fee field altogether from the form. This can be done by simply removing the entire form-group associated with the fee field.The enhanced form now looks like this: