Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
The "upstream" branch is a git mirror of the sfDoctrine2Plugin subversion repository, updated daily. The "master" branch is my fork of the sfDoctrine2Plugin, containing bug fixes and features that I plan on submitting upstream.
branch: master

This branch is 37 commits ahead of blt04:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
config
data/generator
i18n
lib
tests
web
.gitmodules
README

README

# sfDoctrinePlugin 2.0

## Installing

Install the plugin as Git submodule:

    $ git submodule add git://github.com/maxim-oleinik/sfDoctrine2Plugin.git plugins/sfDoctrine2Plugin

    // Init plugins submodules
    $ cd plugins/sfDoctrine2Plugin/lib/vendor/doctrine2
    $ git submodule update --init

Now you just need to enable the plugin in your ProjectConfiguration:

    [php]
    class ProjectConfiguration extends sfProjectConfiguration
    {
      public function setup()
      {
        $this->enablePlugins('sfDoctrine2Plugin');
      }

      /**
       * Setup sfDoctrine2Plugin
       * Customize generator properties
       */
      public function setupPlugins()
      {
        sfConfig::set('sf_generator_proxy_dir', sfConfig::get('sf_lib_dir').'/App/Model/Proxy');
        sfConfig::set('sf_generator_proxy_ns',  'App\Model\Proxy');
        sfConfig::set('sf_generator_form_dir',  sfConfig::get('sf_lib_dir').'/App/Form');
        sfConfig::set('sf_generator_form_ns',   'App\Form');
      }
    }

## Configure Databases

Sample `databases.yml` for MySQL:

    [yml]
    all:
      doctrine:
        class: sfDoctrineDatabase
        param:
          options:
            driver: pdo_mysql
            user: root
            password:
            dbname: doctrine

Sample `databases.yml` for Sqlite:

    [yml]
    all:
      doctrine:
        class: sfDoctrineDatabase
        param:
          options:
            driver: pdo_sqlite
            path: %SF_DATA_DIR%/database.sqlite

## Schema Files

You can specify your schema files in `config/doctrine` just like normal. The schema syntax
is slightly different from Doctrine 1. Below is an example:

    [yml]
    Entities\User:
      type: entity
      table: user
      id:
        id:
          type: integer
          generator:
            strategy: AUTO
      fields:
        username:
          type: string
          length: 255
        password:
          type: string
          length: 255

## Custom Repository Class

You can configure a custom repository class so you can add new methods for
executing and retrieving queries.

    [yml]
    Entities\User:
      type: entity
      table: user
      repositoryClass: UserRepository
    # ...

Now define a `UserRepository` class:

    [php]
    class UserRepository extends EntityRepository
    {
      public function getActiveUsers()
      {
        $qb = $this->createQueryBuilder('u');
        $q = $qb->getQuery();

        return $q->execute();
      }
    }

Now you can use this method like the following:

    [php]
    $repository = $em->getRepository('Entities\User');
    $users = $repository->getActiveUsers();


## Data Fixtures

YAML data fixtures no longer exist in Doctrine 2. Instead we just
use regular PHP code to load our data fixtures.

    [php]
    // data/fixtures/fixtures.php

    $em = $this->getEntityManager();

    $admin = new \Entities\User();
    $admin->username = 'admin';
    $admin->password = 'changeme';

The `$em` variable that is defined in the fixture files is the entity manager that
will be used to persist the entities.

## Building Doctrine

Build models, forms, filters, database and load data fixtures.

    $ php symfony doctrine:build --all --and-load

## Updating Schema

Updating your schema is just a matter of changing your mapping information in
`config/doctrine` and then running the following command:

    $ php symfony doctrine:build --all-classes --and-update-schema

## Entity Manager in Actions

    [php]
    class usersActions extends sfActions
    {
      public function executeIndex()
      {
        $em = $this->getEntityManager();
        $qb = $em->createQueryBuilder();

        // ...
      }
    }

## Migrations

See http://www.doctrine-project.org/projects/migrations/2.0/docs/reference/introduction/en
Symfony tasks:

    $ ./symfony list migrations

Custom config

    $ cp ./plugins/sfDoctrine2Plugin/config/migrations.yml config/migrations.yml
Something went wrong with that request. Please try again.