PayPal - The safer, easier way to pay online!

PHP doctrine projects - 5 minutes starter guide

Create a project

First, prepare Maven and follow the preparation instructions.

Next, type the following Maven goal in your command line and execute it:

 			
mvn archetype:generate \
-DarchetypeGroupId=org.phpmaven \
-DarchetypeArtifactId=php5-doctrine-archetype \
-DarchetypeVersion=2.0-SNAPSHOT \
-DgroupId=org.sample \
-DartifactId=my-app \
-Dversion=0.0.1-SNAPSHOT
			

After some time maven will ask to confirm the parameters. The result will look like this:

my-app 
	|-- pom.xml 
	`-- src 
		`-- main 
			 |-- php 
			 |  `-- bootstrap_doctrine.php
			 |  `-- bootstrap.php
			 |  `-- cli-config.php
			 |  `-- cli-tools.php
			 |  |-- MyApp
			 |  |    `-- Entities
			 |  |         `-- MyModel.php
			 |  `-- my-app.php
			 `-- resources
			    `-- config
			         `-- doctrine.ini
			
  • src/main/php/lib - contains the project source code
  • src/main/php/resources - contains the additional resources; all non-php files like yaml, xml, etc
  • src/test/php - contains the test source code
  • pom.xml The project's Project Object Model, or POM, looks like this:

The POM

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.phpmaven</groupId>
        <artifactId>php-parent-pom</artifactId>
        <version>2.0-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <phpunit.version>3.6.10</phpunit.version>
        <!-- doctrine version -->
        <doctrine.orm.version>2.2.0</doctrine.orm.version>
        <!-- also needs to reflect the real root folder for your autoloading classes -->
        <doctrine.classloader.name>MyApp</doctrine.classloader.name>
        <!-- only use relative path from app root -->
        <doctrine.classloader.base>.</doctrine.classloader.base>
    </properties>

    <groupId>org.sample</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>php</packaging>

    <name>PHP 5 Doctrine Project</name>
    <description>Simple PHP 5 doctrine project.</description>
    <url>http://www.php-maven.org</url>
    <inceptionYear>2012</inceptionYear>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- enable doctrine dev mode -->
                <doctrine.dev.mode>true</doctrine.dev.mode>
                <!-- use maven supplied doctrine -->
                <doctrine.directory>../php-deps/pear</doctrine.directory>
                <!-- sqlite for dev, use relative path from app root for db file-->
                <doctrine.db.driver>sqlite</doctrine.db.driver>
                <doctrine.db.path>../../myapp.sqlite</doctrine.db.path>
            </properties>
        </profile>

        <profile>
            <id>production</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <!-- disable doctrine dev mode -->
                <doctrine.dev.mode>false</doctrine.dev.mode>
                <!-- use pear supplied doctrine-->
                <doctrine.directory>pear</doctrine.directory>
                <!-- mysql for production -->
                <doctrine.db.driver>mysql</doctrine.db.driver>
                <doctrine.db.host>localhost</doctrine.db.host>
                <doctrine.db.port>3306</doctrine.db.port>
                <doctrine.db.name>doctrine_db</doctrine.db.name>
                <doctrine.db.user>doctrine_user</doctrine.db.user>
                <doctrine.db.password>foobar</doctrine.db.password>
            </properties>
        </profile>
    </profiles>
.
.
.

What have I just done?

What have I just done?

You have just executed the Maven goal archetype:generate and passed various parameters to that goal. The prefix archetype is the plugin containing the goal. This goal created a default project structure for your PHP project.

Build the project

Switch to your sample project directory

cd my-app

and run:

mvn package

The command line will print out various actions, ending with the following:

 ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Thu Jul 20 19:15:06 CDT 2008
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------			
			

Maven has created a phar package under the project's target folder.

What has Maven done?

"Maven for PHP" validates the PHP code with the php.exe, runs PHPUnit tests, and creates a phar for the web project.

Setup the database schema

Your project is almost ready to run now, all that is left is creating the database schema, switch to the unified app root

cd target/classes
. Now run
php cli-tools.php orm:schema-tool:create
The default profile is configured to use sqlite as database so no further configuration is needed. A sample configuration for mysql can be found in the production profile. To activate the profile when building the project use
mvn package -Pproduction

Run the app

We can now run the app with

php my-app.php

Documentation

Go to the "Documentation" section.