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 following is a simple stripped back model class which will process and store information about customers in our simple example. Save this file to /app/models/Customer.php.

<?php
class Customer extends \Phalcon\Mvc\Model
{
    protected $id;
    protected $firstname;
    protected $surname;
    
    public function getId()
    {
        return $this->id;
    }
    
    public function getFirstname()
    {
        return $this->firstname;
    }
    
    public function setFirstname($fn)
    {
        $this->firstname = $fn;
    }
    
    public function getSurname()
    {
        return $this->surname;
    }    
    
    public function setSurname($sn)
    {
        $this->surname = $sn;
    }
    
}
?>

This simple class has not been scaffolded or auto-generated in any way. Anyone familiar with building objects using PHP will recognise the simple attributes with getters and setters.

The challenge when using objects like this to process information comes when you decide to "persist" or save the object data. In early OOP the approach was to build a DAO layer which would use SQL to persist the data to a database. MVC systems generally use an Object Relational Mapping system to save data to a SQL (or NoSQL) persistence layer.

Phalcon's ORM is built-in and implicit. This is great for a couple of reasons:

To make the data save to the database you need only add a database table with the same fieldnames as the attribute names in the model class. This is another example of Convention over Configuration.

The job of this createAction is to process the POST data from the form. In our previous examplee this meant simply printing the customer's name to the screen. In a simple PHP application processing the data might mean gathering the data from the form and inserting it into the database through the use of a MYSQL "Insert into" command. Leaving aside connecting to the database and stripping it back as far as possible the code might look something like this.

$fname = $_POST['firstname'];
$sname = $_POST['surname];
$sql = "insert into customer values('$sname','$fname');
$mysqli_execute($dbcon,$sql);

Using the MVC approach we have a Customer object. If we create a new instance of this object from the Model class we can easily save this customer to the database without using SQL.

The equivalent code in Phalcon would be 

$customer = new Customer(); //creates a new instance of the customer object
$customer->setFirstname($this->request->getPost('firstname'));//sets the attributes
$customer->setSurname($this->request->getPost('surname'));//sets the attributes
$customer->save();//persists the Customer object to the database

$this->request->getPost('firstname') is equivalent to $_POST['firstname'] but it's using an Object Oriented approach. The the getter methods of the $request object are used to access the necessary information which are in turn passed to the setter methods of the $customer object. Once we have set the customer's attributes we can save the customer by calling the save() method on the object.

The lack of explicit settings and set-up also greatly simplifies Phalcon and makes it easier to get up and running. As learners become more comfortable with MVC frameworks it is likely that they will start to use other tools that use explicit ORM settings. This is worthwhile as ORMs are an important piece of the puzzle and setting one up can enhance your understanding of the whole picture.

To get our example to work we still need a database. Execute the following script in MySQL

drop database if exists shop;
create database shop;
use shop;
create table customer (
  id int auto_increment,
  firstname varchar(30),
  surname varchar(30),
  primary key(id)
);

Edit the file /app/config/config.php and change the dbname credential from "test" to "shop". 

To demonstrate the flow of the above action-view-action modify the createAction() funciton in /app/controllers/CustomerController.php to include the code which will instantiate, modify and save a Customer object. The CustomerController should now look like this:

<?php
class CustomerController extends ControllerBase
{
    public function newAction()
    {
        
    }
    
    public function createAction()
    {
        $customer = new Customer(); //creates a new instance of the customer object
        $customer->setFirstname($this->request->getPost('firstname'));//sets the attributes
        $customer->setSurname($this->request->getPost('surname'));//sets the attributes
        $customer->save();//persists the Customer object to the database
    }
}
?>

Visit http://localhost/shop/customer/new and submit some test data. Now return and query the database table to verify that your new customer has been added to the database.