
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
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" } }
Run
composer install
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)
Both Propel and VertabeloPropel are now downloaded into the vendor directory.
Create and Setup Your Database
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:
Here is a sample database model which I prepared earlier:
Download the model as an XML file and save it in your project directory as model.xml.
Create a MySQL database.
from the command prompt enter:
create database bookshop;
or in phpMyAdmin:
In Vertabelo generate the SQL create script. Download the script and save it in your project directory.
Run the SQL create script against your database. Either from the command prompt:
\. /home/akozubek/propel-example/sql/create.sql
or in phpMyAdmin:
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
Generate the Propel schema with VertabeloPropel:
vendor/bin/vertabelo-propel.php
or in Windows:
vendor\bin\vertabelo-propel.php.bat
Generate the Propel classes:
vendor/bin/propel model:build
or in Windows:
vendor\bin\propel.bat model:build
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.
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
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.