# Blameable behavior extension for Doctrine 2
**Blameable** behavior will automate the update of username or user reference fields
on your Entities or Documents. It works through annotations and can update
fields on creation, update, property subset update, or even on specific property value change.
This is very similar to Timestampable but sets a string or user object for a user association.
If you map the blame onto a string field, this extension will try to assign the user name.
If you map the blame onto a association field, this extension will try to assign the user
object to it.
Note that you need to set the user on the BlameableListener (unless you use the
Symfony2 extension which does automatically assign the current security context
user).
Features:
- Automatic predefined user field update on creation, update, property subset update, and even on record property changes
- ORM and ODM support using same listener
- Specific annotations for properties, and no interface required
- Can react to specific property or relation changes to specific value
- Can be nested with other behaviors
- Annotation, Yaml and Xml mapping support for extensions
**Symfony:**
- **Blameable** is available as [Bundle](http://github.com/stof/StofDoctrineExtensionsBundle)
for **Symfony2**, together with all other extensions
This article will cover the basic installation and functionality of **Blameable** behavior
Content:
- [Including](#including-extension) the extension
- Entity [example](#entity-mapping)
- Document [example](#document-mapping)
- [Yaml](#yaml-mapping) mapping example
- [Xml](#xml-mapping) mapping example
- Advanced usage [examples](#advanced-examples)
- Using [Traits](#traits)
## Setup and autoloading
Read the [documentation](http://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/annotations.md#em-setup)
or check the [example code](http://github.com/l3pp4rd/DoctrineExtensions/tree/master/example)
on how to setup and use the extensions in most optimized way.
## Blameable Entity example:
### Blameable annotations:
- **@Gedmo\Mapping\Annotation\Blameable** this annotation tells that this column is blameable
by default it updates this column on update. If column is not a string field or an association
it will trigger an exception.
Available configuration options:
- **on** - is main option and can be **create, update, change** this tells when it
should be updated
- **field** - only valid if **on="change"** is specified, tracks property or a list of properties for changes
- **value** - only valid if **on="change"** is specified and the tracked field is a single field (not an array), if the tracked field has this **value**
then it updates the blame
**Note:** that Blameable interface is not necessary, except in cases there
you need to identify entity as being Blameable. The metadata is loaded only once then
cache is activated
Column is a string field:
``` php
id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
public function setBody($body)
{
$this->body = $body;
}
public function getBody()
{
return $this->body;
}
public function getCreatedBy()
{
return $this->createdBy;
}
public function getUpdatedBy()
{
return $this->updatedBy;
}
public function getContentChangedBy()
{
return $this->contentChangedBy;
}
}
```
Column is an association:
``` php
id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
public function setBody($body)
{
$this->body = $body;
}
public function getBody()
{
return $this->body;
}
public function getCreatedBy()
{
return $this->createdBy;
}
public function getUpdatedBy()
{
return $this->updatedBy;
}
public function getContentChangedBy()
{
return $this->contentChangedBy;
}
}
```
## Blameable Document example:
``` php
id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
public function getCreatedBy()
{
return $this->createdBy;
}
public function getUpdatedBy()
{
return $this->updatedBy;
}
}
```
Now on update and creation these annotated fields will be automatically updated
## Yaml mapping example:
Yaml mapped Article: **/mapping/yaml/Entity.Article.dcm.yml**
```
---
Entity\Article:
type: entity
table: articles
id:
id:
type: integer
generator:
strategy: AUTO
fields:
title:
type: string
length: 64
createdBy:
type: string
gedmo:
blameable:
on: create
updatedBy:
type: string
gedmo:
blameable:
on: update
```
## Xml mapping example
``` xml
```
## Advanced examples:
### Using dependency of property changes
Add another entity which would represent Article Type:
``` php
id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
}
```
Now update the Article Entity to reflect publishedBy on Type change:
``` php
type = $type;
}
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
public function getCreatedBy()
{
return $this->createdBy;
}
public function getUpdatedBy()
{
return $this->updatedBy;
}
public function getPublishedBy()
{
return $this->publishedBy;
}
}
```
Yaml mapped Article: **/mapping/yaml/Entity.Article.dcm.yml**
```
---
Entity\Article:
type: entity
table: articles
id:
id:
type: integer
generator:
strategy: AUTO
fields:
title:
type: string
length: 64
createdBy:
type: string
gedmo:
blameable:
on: create
updatedBy:
type: string
gedmo:
blameable:
on: update
publishedBy:
type: string
gedmo:
blameable:
on: change
field: type.title
value: Published
manyToOne:
type:
targetEntity: Entity\Type
inversedBy: articles
```
Now few operations to get it all done:
``` php
setTitle('My Article');
$em->persist($article);
$em->flush();
// article: $createdBy, $updatedBy were set
$type = new Type;
$type->setTitle('Published');
$article = $em->getRepository('Entity\Article')->findByTitle('My Article');
$article->setType($type);
$em->persist($article);
$em->persist($type);
$em->flush();
// article: $publishedBy, $updatedBy were set
$article->getPublishedBy(); // the user that published this article
```
Easy like that, any suggestions on improvements are very welcome
## Traits
You can use blameable traits for quick **createdBy** **updatedBy** string definitions
when using annotation mapping.
**Note:** this feature is only available since php **5.4.0**. And you are not required
to use the Traits provided by extensions.
``` php