propel-bundle/README.markdown

193 lines
5.5 KiB
Markdown
Raw Normal View History

2011-01-26 22:02:01 +01:00
Propel Bundle
=============
This is a (work in progress) implementation of Propel in Symfony2.
2011-01-26 22:02:01 +01:00
Currently supports:
* Generation of model classes based on an XML schema (not YAML) placed under `BundleName/Resources/*schema.xml`.
2011-02-02 20:44:50 +01:00
* Insertion of SQL statements.
* Runtime autoloading of Propel and generated classes.
2011-01-26 22:02:01 +01:00
* Propel runtime initialization through the XML configuration.
2011-02-02 20:44:50 +01:00
* Migrations [Propel 1.6](http://www.propelorm.org/wiki/Documentation/1.6/Migrations).
2011-03-25 17:39:39 +01:00
* Reverse engineering from [existing database](http://www.propelorm.org/wiki/Documentation/1.6/Existing-Database).
2011-02-02 02:29:46 +01:00
2011-01-26 22:02:01 +01:00
Installation
------------
* Clone this bundle in the `src/Propel` directory:
2011-01-26 22:02:01 +01:00
> git submodule add https://github.com/willdurand/PropelBundle.git src/Propel/PropelBundle
2011-02-02 20:44:50 +01:00
* Checkout Propel and Phing in the `vendor` directory:
2011-03-25 17:39:39 +01:00
> svn checkout http://svn.propelorm.org/branches/1.6 vendor/propel
2011-01-30 01:55:15 +01:00
2011-03-25 17:39:39 +01:00
> svn checkout http://phing.mirror.svn.symfony-project.com/tags/2.3.3 vendor/phing
2011-01-30 01:55:15 +01:00
2011-02-03 21:51:03 +01:00
* Instead of using svn, you can clone the unofficial Git repositories:
2011-02-03 21:40:03 +01:00
> git submodule add https://github.com/Xosofox/phing vendor/phing
2011-02-03 21:40:03 +01:00
> git submodule add https://github.com/Xosofox/propel1.6 vendor/propel
2011-02-03 21:40:03 +01:00
* Register this bundle in the `AppKernel` class:
2011-02-02 21:02:56 +01:00
2011-02-03 21:40:03 +01:00
public function registerBundles()
{
$bundles = array(
...
// PropelBundle
new Propel\PropelBundle\PropelBundle(),
2011-02-03 21:40:03 +01:00
// register your bundles
new Sensio\HelloBundle\HelloBundle(),
);
...
}
2011-02-02 21:02:56 +01:00
2011-02-14 22:51:43 +01:00
* Don't forget to register the PropelBundle namespace in `app/autoload.php`:
$loader->registerNamespaces(array(
...
'Propel' => __DIR__.'/../src',
));
2011-02-02 20:44:50 +01:00
2011-01-26 22:02:01 +01:00
Sample Configuration
--------------------
### Project configuration
# in app/config/config.yml
2011-02-23 16:32:48 +01:00
propel:
path: "%kernel.root_dir%/../vendor/propel"
phing_path: "%kernel.root_dir%/../vendor/phing"
2011-02-02 21:06:20 +01:00
# charset: "UTF8"
2011-01-26 22:02:01 +01:00
# in app/config/config*.yml
2011-02-23 16:32:48 +01:00
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: {}
2011-01-26 22:02:01 +01:00
2011-02-02 20:44:50 +01:00
2011-01-26 22:02:01 +01:00
### Sample Schema
2011-02-02 20:44:50 +01:00
Place the following schema in `src/Sensio/HelloBundle/Resources/config/schema.xml` :
2011-01-26 22:02:01 +01:00
<?xml version="1.0" encoding="UTF-8"?>
2011-02-02 20:44:50 +01:00
<database name="default" namespace="Sensio\HelloBundle\Model" defaultIdMethod="native">
2011-01-26 22:02:01 +01:00
2011-02-02 21:06:20 +01:00
<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>
2011-01-26 22:02:01 +01:00
</database>
2011-02-02 20:44:50 +01:00
Commands
--------
2011-01-26 22:02:01 +01:00
### Build Process
2011-02-02 20:44:50 +01:00
Call the application console with the `propel:build` command:
2011-01-26 22:02:01 +01:00
> php app/console propel:build [--classes] [--sql]
2011-01-26 22:02:01 +01:00
2011-02-02 20:44:50 +01:00
### Insert SQL
Call the application console with the `propel:insert-sql` command:
> php app/console propel:insert-sql [--force]
2011-02-02 20:44:50 +01:00
Note that the `--force` option is needed to actually execute the SQL statements.
2011-01-26 22:02:01 +01:00
### Use The Model Classes
2011-02-02 20:44:50 +01:00
Use the Model classes as any other class in Symfony2. Just use the correct namespace, and Symfony2 will autoload them:
2011-01-26 22:02:01 +01:00
class HelloController extends Controller
{
2011-02-02 21:02:56 +01:00
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));
}
2011-01-26 22:02:01 +01:00
}
2011-02-02 20:44:50 +01:00
2011-02-02 02:40:02 +01:00
### Migrations
Generates SQL diff between the XML schemas and the current database structure:
> php app/console propel:migration:generate-diff
2011-02-02 02:40:02 +01:00
Executes the migrations:
> php app/console propel:migration:migrate
2011-02-02 02:40:02 +01:00
2011-02-02 20:44:50 +01:00
Executes the next migration up:
> php app/console propel:migration:migrate --up
2011-02-02 02:40:02 +01:00
2011-02-02 20:44:50 +01:00
Executes the previous migration down:
> php app/console propel:migration:migrate --down
2011-02-02 02:40:02 +01:00
Lists the migrations yet to be executed:
> php app/console propel:migration:status
2011-01-26 22:02:01 +01:00
2011-02-02 20:44:50 +01:00
2011-03-25 17:39:39 +01:00
### Working with existing databases
Run the following command to generate an XML schema from your `default` database:
> php app/console propel:reverse
You can define which connection to use:
> php app/console propel:reverse --connection=default
2011-03-25 19:19:43 +01:00
You can dump data from your database in XML to `app/propel/dump/xml/`:
> php app/console propel:data-dump [--connection[="..."]]
2011-03-25 17:39:39 +01:00
2011-01-26 22:02:01 +01:00
Known Problems
--------------
Your application must not be in a path including dots in directory names (i.e. '/Users/me/symfony/2.0/sandbox/' fails).