Skip to content
jaycc edited this page Oct 23, 2010 · 2 revisions

Doctrine2

Introduction

The Doctrine 2 integration is supported through these components:

  • A Doctrine2 application resource for bootstrapping your Doctrine2 environment.
  • A particular build task which gonna generate your entities in the correct path.
  • A Doctrine authentication adapter for use with Zend_Auth and allow authentication with a Doctrine 2 backend.

Resource

Introduction

LoSo_Zend_Application_Resource_Doctrine will initialize Doctrine 2 with the options passed to it.

To use the resource, you must add LoSo_Zend_Application_Resource to your plugins path in your application/configs/application.ini:

pluginPaths.LoSo_Zend_Application_Resource = "LoSo/Zend/Application/Resource"

Cache

resources.doctrine2.cache = apc|memcache|xcache|array(default)

Configure the cache implementation to use to cache metadata information and queries. Available implementations:

  • apc through DoctrineCommonCacheApcCache
  • memcache through DoctrineCommonCacheMemcacheCache
  • xcache through DoctrineCommonCacheXcacheCache
  • array through DoctrineCommonCacheArrayCache (not recommended)

Proxy

resources.doctrine2.proxy.directory = [path]
resources.doctrine2.proxy.namespace = [namespace]

Set the directory and namespace to use for Doctrine generated proxy classes. Default values are:

resources.doctrine2.proxy.directory = APPLICATION_PATH "/data/doctrine/Proxies"
resources.doctrine2.proxy.namespace = Proxies

Connection

resources.doctrine2.connection.[option] = value

Array of options that will be passed to Doctrine2 DBAL factory to initialize connection. See "Doctrine DBAL documentation":http://www.doctrine-project.org/projects/dbal/2.0/docs/reference/configuration/en for more informations. Here is a classical example:

resources.doctrine2.connection.driver = pdo_mysql
resources.doctrine2.connection.host = 127.0.0.1
resources.doctrine2.connection.dbname = sandbox
resources.doctrine2.connection.user = sandbox
resources.doctrine2.connection.password = pass

Metadata

resources.doctrine2.metadata.driver = yaml|xml|php|annotation(default)
resources.doctrine2.metadata.mappingPaths.[Base_Namespace_] = [path]
resources.doctrine2.metadata.entitiesPaths.[Base_Namespace_] = [path]

The first option is the metadata driver implemntation used by Doctrine to load your entities. The mappingPaths option is an array of paths to the entities that will be loaded by the metadata driver.

Finally the entitiesPaths array will be used by the loso:build task to generate your entities classes from your mapping files. See Build task chapter for more information.

Auth adapter

LoSo_zend_Auth_Adapter_Doctrine2 is an authentication adapter that can be used with Zend_Auth. it provides the ability to authenticate against credentials stored in a Doctrine2 entity.

It requires the Doctrine2 entity manager to be passed to the constructor.

The available configuration options include:

  • entityName: This is the name of the entity that contains the authentication credentials.
  • identityField: This is the name of the entity field used to represent the identity such as a username or e-mail address
  • credentialField: This is the name of the entity field used to represent the credential. It most of the time corresponds to the password.

Basic usage:

// Initialize auth adapter
$adapter = new LoSo_Zend_Auth_Adapter_Doctrine2($entityManager);
$adapter->setEntityName('Application_Model_User')
    ->setIdentityField('username')
    ->setCredentialField('password');

// Pass identity and credential to authenticate
// Most of the time retrieved from a posted form.
$adapter->setIdentity('my_username')
    ->setCredential('my_password');

// Authenticate
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);

See "Zend_Auth documentation":http://framework.zend.com/manual/en/zend.auth.html for more informations on authentication in Zend Framework.

Doctrine Console

Introduction

The Doctrine Console is a Command Line Interface tool for simplifying many common commands during the development of a project that uses Doctrine (from Doctrine documentation).

LoSoLib provides an integration of the Doctrine Console into your Zend Framework project.

Installation

Copy the scripts/doctrine2 folder into the corresponding folder in your project tree. Here you are, if you have configured your Doctrine resource as mentioned in the corresponding chapter, you are now able to use the Doctrine Console!

Usage

Go to <project_path>/scripts/doctrine2.

On a *nix system, be sure that you have permission to execute the doctrine file. Then run:

$ ./doctrine

On a Windows system, just run:

> doctrine.bat

For more information on commands available through Doctrine Console tool, go to the "Doctrine Console documentation":http://www.doctrine-project.org/projects/orm/2.0/docs/reference/tools/tools#the-doctrine-console:command-overview.

Build command

The loso:build command is an additional command provided by LoSoLib. It is particularly useful if you want to generate your entity classes from an XML, YAML or plain PHP mapping.

It will use the entitiesPath option of your Doctrine entity to automatically generate your entity classes in the correct path (for example: application/models) with a correct management of undescored namespaced casses.

Usage:

  • loso:build [--generate-annotations[="..."]] [--generate-methods[="..."]] [--regenerate-entities[="..."]] [--update-entities[="..."]] [--extend[="..."]] [--num-spaces[="..."]]

Options:

  • --generate-annotations Flag to define if generator should generate annotation metadata on entities. (default: )

  • --generate-methods Flag to define if generator should generate stub methods on entities. (default: 1)

  • --regenerate-entities Flag to define if generator should regenerate entity if it exists. (default: )

  • --update-entities Flag to define if generator should only update entity if it exists. (default: 1)

  • --extend

    Defines a base class to be extended by generated entity classes.

  • --num-spaces Defines the number of indentation spaces (default: 4)

Example:

In your application.ini:

resources.doctrine2.metadata.driver = yaml
resources.doctrine2.metadata.mappingPaths.Application_Model_ = APPLICATION_PATH "/doctrine/mapping"
resources.doctrine2.metadata.entitiesPaths.Application_Model_ = APPLICATION_PATH "/models"

In application/doctrine/mapping/Application_Model_User.dcm.yml:

Application_Model_User:
    type: entity
    table: user
    id:
        id:
            type: integer
            generator:
                strategy: AUTO
    fields:
        firstname:
            type: string
            length: 50
        lastname:
            type: string
            length: 50
        email:
            type: string
            length: 100
            unique: true
$ ./doctrine loso:build

It will parse the mapping file into application/doctrine/mapping/ directory and generate the corresponding User.php entity classes into application/models.