PHP Hints

November 11, 2010

Doctrine 2 ORM Cookbook example

Filed under: Doctrine ORM,PHP Code — kkruecke @ 2:39 pm
Tags: , , ,

The first portion of the Doctrine 2 ORM Cookbook example is available on sourceforge. See the included README.txt.

Steps to setup the database and config both cli-config.php and cookbook.php. This setup assumes you have used pear to install Doctrine 2. To do so:

UPDATED December 27th, 2010: Doctrine 2 is now no longer in beta

$sudo pear channel-discover pear.doctrine-project.org
$sudo pear install -o pear.doctrine-project.org/DoctrineORM
$sudo pear list -c pear.doctrine-project.org

This should also install the prerequisite packages: DoctrineCommon and DoctrineDBAL.
Note: pear config-set preferred_state beta is no longer necessary because Doctrine 2 is no longer in beta!

1. Generate the schema from the command line using the doctrine command line tool. For example, on Linux

   $ doctrine orm:schema-tool:create --dump-sql > generated.sql

2. Edit generated.sql and add the following lines to the top. Use the database name of your choice.

    drop database if exists bugs_db_cb;
    create database bugs_db_cb;
    use bugs_db_cb;

3. Edit cli-config.php and cookbook.php. Change the $connectionOptions array parameters, specifying the name of the database, the database’s user, and its password. Remember to create a user of the database.

   $connectionOptions = array(
        'driver' => 'pdo_mysql',
        'dbname' => 'bugs_db_cb', // your database name goes here
        'user' => 'bugs_db_cb',     // the db user goes here
        'password' => 'YOUR-PW-HERE',    // the password goes here
        'host' => 'localhost'
        );

4. Make sure that the Proxies subdirectory, the directory specified in the call to setProxyDir is writable by your webserver. This is particularly true when running in “development” mode.

$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');

4. Now, create the database. For example, from the command line

   $ mysql -u root -p > ./generated.sql

5. Confirm that the annotation mappings and the database schema are valid.

   $ doctrine orm:validate-schema

6. This example uses the annoation driver. If you will to generate the xml mappings, do

   $ doctrine orm:convert-mapping xml ./somesubdir
  

6. Finally, run the code

   http:://localhost/cookbook
Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.