Back to articles list
- 3 minutes read

How to Develop a PHP+MySQL Application With Propel and Vertabelo

Propel is a popular ORM (Object-Relational Mapping) library for PHP. Recently, the Vertabelo team started to develop VertabeloPropel, a tool which converts your Vertabelo models into Propel’s schema xml.

If you want to try using Propel with Vertabelo to develop a simple PHP MySQL application, here is how to do it. The following example uses Composer, a PHP tool for handling project dependencies.

Set up Your Project

  1. Create the file composer.json which describes your project dependencies. You will use at least Propel (package propel/propel) and VertabeloPropel (vertabelo/vertabelo-propel) as your project dependencies.

    {
        "name": "Vertabelo/ExampleBookshop",
        "description": "Example Bookshop application",
        "require": {
            "propel/propel": "~2.0@dev",
            "vertabelo/vertabelo-propel": "*@dev"
        }
      }
    

  2. Run

    composer install
    

  3. You will see something like this:

    Loading composer repositories with package information
    Installing dependencies (including require-dev)
    …
    Loading composer repositories with package information
    Installing dependencies (including require-dev)
    

  4. Both Propel and VertabeloPropel are now downloaded into the vendor directory.

Create and Setup Your Database

  1. Create your model in Vertabelo. (If you don’t have your account yet, you can sign up for a free trial here.) Choose MySQL as your database engine:

    Create a new database model for MySQL database

    Here is a sample database model which I prepared earlier:




  2. Download the model as an XML file and save it in your project directory as model.xml.

    Exporting your database model to an XML file in Vertabelo

  3. Create a MySQL database.

    from the command prompt enter:

    create database bookshop;
    

    or in phpMyAdmin:

    undefined

  4. In Vertabelo generate the SQL create script. Download the script and save it in your project directory.

    Generating an SQL code for your database model in Vertabelo

  5. Run the SQL create script against your database. Either from the command prompt:

    \. /home/akozubek/propel-example/sql/create.sql
    

    or in phpMyAdmin:

    undefined

  6. Create the file propel.yaml describing the location of your MySQL database.

      database:
          connections:
              bookshop:
                  adapter: mysql
                  classname: Propel\Runtime\Connection\ConnectionWrapper
                  dsn: "mysql:host=localhost;dbname=bookshop"
                  user: bookshop
                  password:
                  attributes:
      runtime:
          defaultConnection: bookshop
          connections:
              - bookshop
      generator:
          defaultConnection: bookshop
          connections:
              - bookshop
    

Generate Propel Classes

  1. Generate the Propel schema with VertabeloPropel:

    vendor/bin/vertabelo-propel.php
    

    or in Windows:

    vendor\bin\vertabelo-propel.php.bat
    

  2. Generate the Propel classes:

    vendor/bin/propel model:build
    

    or in Windows:

    vendor\bin\propel.bat model:build
    

  3. The generated Propel classes will be in the directory generated-classes. You then have to autoload them. For example, you can do this with composer by adding this entry to your composer.json file:

    {
      ...
      "autoload": {
        "classmap": ["generated-classes/"]
      }
    }
    

    and then executing the command composer dump-autoload.

  4. Convert the propel.yaml file into the config.php file, which you can later include in your PHP files

      
    vendorbin\propel.bat config:convert
    

    or

    vendor/bin/propel config:convert
    

  5. Write your application using Propel classes. Here is the official documentation for your reference: http://propelorm.org/documentation/03-basic-crud.html.

    You can always take a look at our example bookshop application at the VertabeloPropel GitHub repository.

go to top