propel-bundle/README.markdown
2011-02-02 21:06:20 +01:00

4.6 KiB

Propel Bundle

This is a (work in progress) implementation of Propel in Symfony2.

Currently supports:

  • Generation of model classes based on an XML schema (not YAML) placed under BundleName/Resources/*schema.xml.
  • Insertion of SQL statements.
  • Runtime autoloading of Propel and generated classes.
  • Propel runtime initialization through the XML configuration.
  • Migrations Propel 1.6.

Installation

  • Clone this bundle in the vendor/symfony/src/Symfony/Bundle directory:

    git submodule add git@github.com:willdurand/PropelBundle.git vendor/symfony/src/Symfony/Bundle/PropelBundle

  • Checkout Propel and Phing in the vendor directory:

    cd vendor

    svn checkout http://svn.propelorm.org/branches/1.6 propel

    svn checkout http://phing.mirror.svn.symfony-project.com/tags/2.3.3 phing

  • Register it your AppKernel class:

     public function registerBundles()
     {
         $bundles = array(
             ...
    
             // PropelBundle
             new Symfony\Bundle\PropelBundle\PropelBundle(),
             // register your bundles
             new Sensio\HelloBundle\HelloBundle(),
         );
    
         ...
     }
    

Sample Configuration

Project configuration

# in ./app/config/config.yml
propel.config:
    path:       %kernel.root_dir%/../src/vendor/propel
    phing_path: %kernel.root_dir%/../src/vendor/phing
#    charset:   "UTF8"

propel.dbal:
    driver:               mysql
    user:                 root
    password:             null
    dsn:                  mysql:host=localhost;dbname=test
    options:              {}
#    default_connection:       default
#    connections:
#      default:
#        driver:               mysql
#        user:                 root
#        password:             null
#        dsn:                  mysql:host=localhost;dbname=test
#        options:              {}

Sample Schema

Place the following schema in src/Sensio/HelloBundle/Resources/config/schema.xml :

<?xml version="1.0" encoding="UTF-8"?>
<database name="default" namespace="Sensio\HelloBundle\Model" defaultIdMethod="native">

    <table name="book">
        <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
        <column name="title" type="varchar" primaryString="1" size="100" />
        <column name="ISBN" type="varchar" size="20" />
        <column name="author_id" type="integer" />
        <foreign-key foreignTable="author">
            <reference local="author_id" foreign="id" />
        </foreign-key>
    </table>

    <table name="author">
        <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
        <column name="first_name" type="varchar" size="100" />
        <column name="last_name" type="varchar" size="100" />
    </table>

</database>

Commands

Build Process

Call the application console with the propel:build command:

> php hello/console propel:build [--classes] [--sql]

Insert SQL

Call the application console with the propel:insert-sql command:

> php hello/console propel:insert-sql [--force]

Note that the --force option is needed to actually execute the SQL statements.

Use The Model Classes

Use the Model classes as any other class in Symfony2. Just use the correct namespace, and Symfony2 will autoload them:

class HelloController extends Controller
{
    public function indexAction($name)
    {
        $author = new \Sensio\HelloBundle\Model\Author();
        $author->setFirstName($name);
        $author->save();

        return $this->render('HelloBundle:Hello:index.html.twig', array('name' => $name, 'author' => $author));
    }
}

Migrations

Generates SQL diff between the XML schemas and the current database structure:

> php hello/console propel:migration:generate-diff

Executes the migrations:

> php hello/console propel:migration:migrate

Executes the next migration up:

> php hello/console propel:migration:migrate --up

Executes the previous migration down:

> php hello/console propel:migration:migrate --down

Lists the migrations yet to be executed:

> php hello/console propel:migration:status

Known Problems

Your application must not be in a path including dots in directory names (i.e. '/Users/me/symfony/2.0/sandbox/' fails).