Development

sfDoctrinePlugin1.1

You must first sign up to be able to contribute.

Version 18 (modified by hansbrix, 10 years ago)
added doctrine-build-model note to schema section

sfDoctrinePlugin

This wiki page is a shared wiki page for sfDoctrinePlugin1.0 and sfDoctrinePlugin1.1. The functionality is kept identical but each branch is compatible with a different version of symfony.

Installation

Checkout from svn

cd plugins

// For the symfony 1.0 compatible branch
svn co http://svn.symfony-project.com/plugins/sfDoctrinePlugin/branches/1.0 sfDoctrinePlugin

// For the symfony 1.1 compatible branch
svn co http://svn.symfony-project.com/plugins/sfDoctrinePlugin/branches/1.1 sfDoctrinePlugin

cd ..

Configure your databases.yml in config/databases.yml. Be sure to change class to sfDoctrineDatabase

all:
  db_name:
    class:          sfDoctrineDatabase
    param:
      dsn:          mysql://user:pass@localhost/db_name

Clear your cache

symfony cc

Now you may begin creating your schema files and fixtures and use the symfony doctrine tasks to create your database, build your models, build/insert sql, and load fixtures. Below you will find example schema files and fixtures and links to the Doctrine manual for more detailed information.

Doctrine Schemas

Below is a link to the chapter about schema files in the Doctrine manual and a simple schema example. More detailed information and examples can be located in the Doctrine Manual. The file should be placed inside config/doctrine/schema.yml.

A Pake task exists to create doctrine model files from your schema.yml. use the following:

symfony doctrine-build-model
symfony cc

Your model files will now appear in project/lib/model/doctrine.

http://www.phpdoctrine.org/index.php/documentation/manual?chapter=schema-files

---
Group:
# bind this model to connection1
  connection: connection1
  columns:
    id:
      notnull: true
      primary: true
      autoincrement: true
      type: integer
      length: 4
      name: id
    name:
      type: string
      length: 255
  relations:
    Users:
      class: User
      refClass: UserGroup
# set attributes on the model
  attributes:
    export: tables
# you can set templates on your schema with the following syntax
# if you do not require any options to be set for the template
# actAs and templates are the same, actAs serves as a convenience method for the Templates/Plugins
# that come bundled with Doctrine
  templates: [Doctrine_Template_NestedSet, Doctrine_Template_Versionable]
# this below syntax can be used for the templates above
  actAs:
    NestedSet:
      hasManyRoots: true
      rootColumnName: root_id
UserGroup:
  columns:
    user_id:
      type: integer
      length: 4
      primary: true
    group_id:
      type: integer
      length: 4
      primary: true
  relations:
    User: -
    Group: -

Doctrine Fixture Files

Below is the link to the chapter about doctrine fixture files and a simple example. More detailed information and examples can be found in the Doctrine manual.

http://www.phpdoctrine.org/index.php/documentation/manual?chapter=data-fixtures

---
Adult:
  Adult_1:
    name: Parent 1
    Contact: Contact_1
Car:
  Car_1:
    name: Chevrolet Trailblazer
  Car_2:
    name: Chevrolet Blazer
  Car_3:
    name: Buick
Child:
  Child_1:
    name: Child 1
    Adult: Adult_1
Contact:
  Contact_1:
    name: Jonathan H. Wage
  Contact_3:
    name: Daniel Adams
  Contact_4:
    name: Robert Adams

Plugin Schemas

Doctrine is able to handle the model generation in sfDoctrinePlugin natively.

Example:

---
PluginModel:
  columns:
    name:
      type: string
      size: 255

Creating/Dropping Databases

Currently the creating and dropping database functionality requires you to use your database name as the connection name. This is necessary because PDO does not offer anyway to retrieve the name of the database you are connected to from a PDO instance.

Example Usage

$user = new User();
$user->username = 'jonwage';
$user->save();

$userTable = Doctrine::getTable('User');

$user = $userTable->find(1);
$user = $userTable->findById(1);
$user = $userTable->findByUsername('jonwage');

$user = $userTable->find(1);
$user->delete();

$query = new Doctrine_Query();
$user = $query->from('User u')->where("u.username = 'jonwage'")->limit(1)->execute()->getFirst();

$query = new Doctrine_Query();
$query->delete('User')->from('User u')->where("u.username = 'jonwage'")->execute();

/* Do a Transaction */
Doctrine_Manager::getInstance()->getCurrentConnection()->beginTransaction();

$user = new User();
$user->username = 'jonwage';
$user->save();

$user = new User();
$user->username = 'isleshocky77';
$user->save();

// Neither have been written to the database yet
Doctrine_Manager::getInstance()->getCurrentConnection()->commit();
// Both have now been written to the database

Using Doctrine in Batch files

define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/..'));
define('SF_APP',         'public');
define('SF_ENVIRONMENT', 'cli');
define('SF_DEBUG',       false);
 
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');

# This is needed to initialize the db connections
sfContext::getInstance()->getDatabaseManager()->initialize();

Known problems

class xxx not found during task execution

This error usually occurs because of old data in the cache. Clearing the cache with

symfony cc

should help.

In some very stubborn cases the following might help: Symfony 1.1 stores some information about autoloading in your /tmp directory which you can safely delete. It will be recreated as soon as you reload a page or execute a Symfony pake task.

To delete this file under Linux you may type:

rm /tmp/sf_autoload_cmd_112b7ba72e740108ee67fecc6159a54d.data

where the "112b7ba72e740108ee67fecc6159a54d" will vary.