Download any version of Doctrine from the SVN server http://svn.doctrine-project.org
Check out a specific version:
svn co http://svn.doctrine-project.org/branches/1.2 .
To update Doctrine execute the following command from your terminal:
svn update
Navigating to your checked out project:
cd /var/www/my_project
Setup Doctrine as an SVN external:
svn propedit svn:externals lib/vendor
The above command will open editor. Place the following text inside and save:
doctrine http://svn.doctrine-project.org/branches/1.2/lib
Install Doctrine by doing an svn update:
svn update
It will download and install Doctrine at the following path: /var/www/my_project/lib/vendor/doctrine
Install Doctrine with the following command:
pear install pear.doctrine-project.org/Doctrine-1.2.x
For more details check the documentation.
Find the Doctrine.php
file containing the core class in the lib folder where you downloaded Doctrine.
Move doctrine libraries to a folder in your project:
mkdir lib
mkdir lib/vendor
mkdir lib/vendor/doctrine
mv /path/to/doctrine/lib doctrine
Or you can use externals:
svn co http://svn.doctrine-project.org/branches/1.2/lib lib/vendor/doctrine
Add it to your svn externals:
svn propedit svn:externals lib/vendor
This will open up your editor. Place the following inside and save:
doctrine http://svn.doctrine-project.org/branches/1.2/lib
When you do SVN update you will get the Doctrine libraries updated:
svn update lib/vendor
Create a file named bootstrap.php
and place the following code in the file:
// bootstrap.php
/* Bootstrap Doctrine.php, register autoloader specify
configuration attributes and load models. */
require_once(dirname(**FILE**) . '/lib/vendor/doctrine/Doctrine.php');
Register the class :php:class:`Doctrine`
autoloader function in the bootstrap file:
// bootstrap.php
spl_autoload_register(array('Doctrine', 'autoload'));
Create the singleton :php:class:`Doctrine_Manager`
instance and assign it to a variable named $manager
:
// bootstrap.php
$manager = Doctrine_Manager::getInstance();
For more details check the documentation.
Modify bootstrap.php
to use the MySQL database:
// bootstrap.php
$conn = Doctrine_Manager::connection('mysql://root:mys3cr3et@localhost/doctrinetest', 'doctrine');
You can use the :php:meth:`$conn->createDatabase`
method to create the database if it does not already exist.
Storage file for created models:
mkdir doctrine_example/models
Generate models using PHP script:
// test.php
Doctrine_Core::generateModelsFromDb(
'models',
array('doctrine'),
array('generateTableClasses' => true)
);
You can place custom functions inside the classes to customize the functionality of your models. Below are some examples:
// models/User.php
class User extends BaseUser
{
public function setPassword($password)
{
return $this->_set('password', md5($password));
}
}
In order for the above password accessor overriding to work properly you must enable the auto_accessor_override attribute in your bootstrap.php file like done below:
// bootstrap.php
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
Now when you try and set a users password it will be md5 encrypted.
// bootstrap.php
Doctrine_Core::loadModels('models');
Use PHP script:
// example.php
Doctrine_Core::generateYamlFromModels('schema.yml', 'models');
Execute the example.php script:
php example.php
File named schema.yml is created in the root of the project directory.
For more details check the documentation.
To re-generate everything and make sure the database is reinstantiated each time prepare script:
// generate.php
require_once('bootstrap.php');
Doctrine_Core::dropDatabases(); Doctrine_Core::createDatabases();
Doctrine_Core::generateModelsFromYaml('schema.yml', 'models');
Doctrine_Core::createTablesFromModels('models');
Regenerate the database from the schemafiles running the command:
php generate.php
For more details check the documentation.
For installation with Symfony 2.1 and newer select one ofthe following methods:
Add the following snippets to “deps” files:
[doctrine-mongodb]
git=http://github.com/doctrine/dbal.git
[doctrine-mongodb-odm]
git=http://github.com/doctrine/doctrine2.git
[DoctrineBundle]
git=http://github.com/doctrine/DoctrineBundle.git
target=/bundles/Doctrine/Bundle/DoctrineBundle
Add the following dependencies to your projects composer.json file:
"require": {
# ..
"doctrine/doctrine-bundle": "~1.2"
# ..
}
The times of using YAML for data fixtures is no longer. Instead, you are only required to use plain PHP for loading your data fixtures.
// data/fixtures/fixtures.php
$em = $this->getEntityManager();
$admin = new \Models\User();
$admin->username = 'admin';
$admin->password = 'changeme';
Now you’re ready to build everything. The following command will build models, forms, filters, database and load data fixtures.
$ php symfony doctrine:build --all --and-load
If you change your schema mapping information and want to update the database you can easily do so by running the following command after changing your mapping information.
$ php symfony doctrine:build --all-classes --and-update-schema
Doctrine2 uses one *.dcm.xml
schema file for each entity. The file is structured like this:
book:
columns:
id:
cover:
inheritance:
extends: itemRecord
itemRecord:
columns:
id:
name:
item:
relations:
Authors:
author:
columns:
id:
firstName:
lastName:
birthDate:
relations:
ItemRecords:
authorHasitemRecord:
columns:
author_id:
item_record_id:
You can go and see how to do this in few clicks.
Simple entity with a primary key and several fields:
author:
columns:
id:
unique: true
primary: true
type: integer(255)
notnull: true
firstName:
type: string
notnull: true
lastName:
type: string
notnull: true
birthDate:
type: string
relations:
ItemRecords:
Entity with all options defined:
itemRecord:
options:
charset: utf8
type: INNODB
collate: utf8_unicode_ci
tableName: item_record
attributes:
export: none
columns:
id:
unique: true
primary: true
type: integer(255)
publisherId:
type: integer(255)
eanId:
unique: true
type: integer
name:
unique: true
type: string(255)
item:
default: item_new
type: string(255)
indexes:
ix_name_ean:
fields: [name, eanId]
type: unique
ix_name:
fields: [name]
relations:
publisher:
ean:
authors:
You can go and see how to do this in few clicks.
Primary key definition:
id:
unique: true
primary: true
type: integer(255)
notnull: true
autoincrement: true
Primary key with all base properties set:
columns:
id:
unique: true
primary: true
type: integer(255)
notnull: true
autoincrement: true
collation: utf8_unicode_ci
nospace: true
notblank: true
unsigned: true
You can go and see how to do this in few clicks.
Regular field definition:
name:
unique: true
type: string(255)
notnull: true
Regular field with all options set:
columns:
name:
unique: true
type: string(255)
notnull: true
notblank: true
fixed: true
collation: utf8_unicode_ci
List of all validators:
column:
item:
notnull: true
fixed: true
notblank: true
country: false
creditcard: false
date: false
email: false
future: false
htmlcolor: false
ip: false
minlength: 1
nospace: true
past: false
readonly: false
regexp: item_*
range: 0,999
scale: 2
unsigned: true
usstate: false
You can go and see how to do this in few clicks.
Non-unique index:
author:
indexes:
ix_name_last:
fields: [lastName]
Unique index definition:
author:
indexes:
ix_first_name_last_name_date:
fields: [firstName, lastName, birthDate]
type: unique
You can go and see how to do this in few clicks.
One to one owner side:
itemRecord:
columns:
id:
unique: true
primary: true
eanId:
unique: true
type: integer
ean:
class: ean
foreignAlias: itemRecord
local: eanId
foreign: id
One to one inverse side:
ean:
columns:
id:
unique: true
primary: true
unique: true
type: string
Many to one owner side:
itemRecord:
columns:
id:
unique: true
primary: true
publisherId:
type: integer(255)
notnull: true
relations:
publisher:
class: publisher
foreignAlias: itemRecord
local: publisherId
foreign: id
Many to one inverse side:
publisher:
columns:
id:
unique: true
primary: true
Many to one owner side with all properties:
relations:
publisher:
class: publisher
foreignAlias: itemRecord
onDelete: SET NULL
onUpdate: CASCADE
cascade: [(NULL)]
type: one
foreignType: many
local: publisherId
foreign: id
Many to one inverse side with all properties:
relations:
itemRecord:
class: itemRecord
foreignAlias: publisher
onDelete: SET NULL
onUpdate: CASCADE
cascade: [(NULL)]
type: many
foreignType: one
local: id
foreign: publisherId
Many to one owner side using non-PK foreign key:
itemRecord:
columns:
id:
unique: true
primary: true
type: integer(255)
publisher_name:
type: string
publisher_vat_code:
type: string
relations:
Publisher:
class: publisher
foreignAlias: ItemRecord
local: publisher_name
foreign: name
Many to one inverse side using non-PK foreign key:
publisher:
columns:
id:
unique: true
primary: true
type: integer(255)
notnull: true
autoincrement: true
name:
type: string
vatCode:
type: string
notnull: true
You can go and see how to do this in few clicks.
Many to many owner side:
itemRecord:
columns:
id:
unique: true
primary: true
relations:
Authors:
class: author
refClass: authorHasitemRecord
local: item_record_id
foreign: author_id
Many to many inverse side:
author:
columns:
id:
unique: true
primary: true
relations:
ItemRecords:
class: itemRecord
refClass: authorHasitemRecord
local: author_id
foreign: item_record_id
Many to many with all options enabled:
authorHasitemRecord:
columns:
author_id:
primary: true
type: integer(255)
notnull: true
item_record_id:
primary: true
type: integer(255)
notnull: true
relations:
author:
class: author
onDelete: SET NULL
onUpdate: CASCADE
foreignAlias: itemRecord
local: author_id
foreign: id
Many-to-many entity.
authorHasitemRecord:
columns:
author_id:
primary: true
type: integer(255)
notnull: true
item_record_id:
primary: true
type: integer(255)
notnull: true
relations:
author:
class: author
foreignAlias: itemRecord
local: author_id
foreign: id
Many to many owner side using non-PK foreign keys:
itemRecord:
columns:
id:
unique: true
primary: true
publisher_name:
type: string
publisher_vat_code:
type: string
name:
unique: true
type: string(255)
item:
default: item_new
type: string(255)
relations:
Author:
class: author
refClass: author2itemRecord
local: item_record_id
foreign: author_first_name
Many to many inverse side using non-PK foreign key:
author:
columns:
id:
unique: true
primary: true
firstName:
type: string
notnull: true
lastName:
type: string
notnull: true
relations:
ItemRecord:
class: itemRecord
refClass: author2itemRecord
local: author_first_name
foreign: item_record_id
Many to many entity using non-PK foreign keys:
author2itemRecord:
columns:
item_record_id:
primary: true
type: integer(255)
notnull: true
author_first_name:
primary: true
type: string
notnull: true
author_last_name:
primary: true
type: string
notnull: true
You can go and see how to do this in few clicks.
Simple table inheritance parent:
itemRecord:
columns:
id:
unique: true
primary: true
Simple table inheritance child:
book:
columns:
id:
unique: true
primary: true
inheritance:
extends: itemRecord
type: simple
keyField: item
keyValue: book
Concrete inheritance parent:
itemRecord:
columns:
id:
unique: true
primary: true
Concrete inheritance child:
book:
columns:
id:
unique: true
primary: true
inheritance:
extends: itemRecord
type: concrete
keyField: item
keyValue: book
Column aggregation inheritance parent:
itemRecord:
columns:
id:
unique: true
primary: true
Column aggregation inheritance child:
book:
columns:
id:
unique: true
primary: true
inheritance:
extends: itemRecord
type: column_aggregation
keyField: item
keyValue: book
You can go and see how to do this in few clicks.
Blameable behavior will automate the update of username or user reference fields on your Entities or Documents.
actAs:
Blameable:
columns:
created:
disabled: false
length: 8
name: created_by
alias: alias
type: integer
updated:
alias: alias
disabled: false
length: 8
name: updated_by
onInsert: true
type: integer
blameVar: userId
default: false
listener: Doctrine_Template_Listener_Blameable
relations:
created:
class: User
disabled: true
foreign: id
name: CreatedBy
updated:
class: User
disabled: true
foreign: id
name: UpdatedBy
Loggable behavior tracks your record changes and is able to manage versions.
actAs:
EventLoggable:
logger:
path: ..\logs\
type: string
The Geographical behavior can be used with any data record for determining the number of miles or kilometers between 2 records.
actAs:
Geographical:
latitude:
name: latitude
size: 255
type: float
longitude:
name: longitude
size: 255
type: float
For more details check the documentation.
actAs:
GoogleI18n:
fields: [name]
languages: [EN]
I18n translatable behavior offers a very handy solution for translating specific record fields in different languages.
actAs:
I18n:
className: "%CLASS%Translation"
generateFiles: false
length: 2
pluginTable: false
table: false
type: string
For more details check the documentation.
Nested-set behavior will implement the tree behavior on your Entity.
actAs:
NestedSet:
hasManyRoots: true
levelColumnName: name
rootColumnName: id
For more details check the documentation.
The Searchable behavior is a fulltext indexing and searching tool.
actAs:
Searchable:
analyzer: Doctrine_Search_Analyzer_Standard
batchUpdates: false
className: "%CLASS%Index"
generateFiles: false
generatePath: false
pluginTable: false
type: 1
For more details check the documentation.
Sluggable behavior will build the slug of predefined fields on a given field which should store the slug.
actAs:
Sluggable:
alias: slug
builder: Doctrine_Inflector, urlize
canUpdate: false
indexName: sluggable
length: 255
name: slug
type: string
unique: true
uniqueIndex: true
For more details check the documentation.
Sortable behavior will maintain a position field for ordering entities.
actAs:
Sortable:
manyListsColumn: [name, publisherId]
SoftDeleteable behavior allows to “soft delete” objects, filtering them at SELECT time by marking them as with a timestamp, but not explicitly removing them from the database.
actAs:
SoftDelete:
length: 255
name: deleted_at
options:
notnull: false
type: timestamp
For more details check the documentation.
Timestampable behavior will automate the update of date fields on your Entities or Documents.
actAs:
Timestampable:
created:
alias: createdAt
disabled: false
expression: false
format: Y-m-d H:i:s
name: created_at
type: timestamp
updated:
alias: updatedAt
disabled: false
expression: false
name: updated_at
format: Y-m-d H:i:s
onInsert: true
type: timestamp
For more details check the documentation.
actAs:
Taggable:
className: "%CLASS%TaggableTag"
generateFiles: false
pluginTable: false
table: false
tagAlias: Tags
tagClass: Taggabletag
tagField: name
Definitions on this page were modelled and generated by Skipper, visual schema editor for ORM frameworks.
Skipper greatly simplifies work with Doctrine and saves huge amount of time. Every example entity and relation used in this Cheatsheet can be achieved with just a few clicks with Skipper. Generated code is clean and elegant and complies with all coding standards.
To learn how Skipper works visit the product tour.
Skipper allows to model and export the definition for every Doctrine element and its properties. Further advantages of automated export are:
Useful links:
Project export - more info about Skipper definitions export
Export to Doctrine - how to export your changes to definition schema files
Any existing Doctrine project can be simply and quickly imported to Skipper. This enables:
Useful links:
Project import - general information about import feature
Doctrine project import - how to import existing project to Skipper
You can try Skipper during its 14-day evaluation period. Trial version offers full application functionality without any limitation and no credit card is needed.
Download trial version from the tool websites at www.skipper18.com/download.
Do you know any other helpful or interesting sites that should be linked here?
Let us know: developers@ormcheatsheet.com
Found a typo? Something is wrong or missing in the Cheatsheet? Just fork and edit it!
Created by Inventic developed by community
If you want to leave feedback, contact us developers@ormcheatsheet.com
This work is licensed under a
Creative Commons Attribution-NonCommercial 3.0 Unported License