Skip to content

Commit

Permalink
Merge pull request #262 from lopsided/master
Browse files Browse the repository at this point in the history
Timestampable - add db field type parameter
  • Loading branch information
docteurklein committed Dec 18, 2015
2 parents 5bad0a2 + dbe5bfb commit 328c0ec
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,19 @@ so that when you try to call `getName` (for example) it will return you the tran
```

If you wish to change the doctrine type of the database fields that will be created for timestampable models you can
set the following parameter like so:

``` yaml
parameters:
knp.doctrine_behaviors.timestampable_subscriber.db_field_type: datetimetz
```

`datetimetz` here is a useful one to use if you are working with a Postgres database, otherwise you may encounter some
timezone issues. For more information on this see:
<a href="http://doctrine-dbal.readthedocs.org/en/latest/reference/known-vendor-issues.html#datetime-datetimetz-and-time-types">http://doctrine-dbal.readthedocs.org/en/latest/reference/known-vendor-issues.html#datetime-datetimetz-and-time-types</a>

The default type is `datetime`.

<a name="blameable" id="blameable"></a>
### blameable
Expand Down
2 changes: 2 additions & 0 deletions config/orm-services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ parameters:
knp.doctrine_behaviors.softdeletable_subscriber.softdeletable_trait: Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletable
knp.doctrine_behaviors.timestampable_subscriber.class: Knp\DoctrineBehaviors\ORM\Timestampable\TimestampableSubscriber
knp.doctrine_behaviors.timestampable_subscriber.timestampable_trait: Knp\DoctrineBehaviors\Model\Timestampable\Timestampable
knp.doctrine_behaviors.timestampable_subscriber.db_field_type: datetime
knp.doctrine_behaviors.blameable_subscriber.class: Knp\DoctrineBehaviors\ORM\Blameable\BlameableSubscriber
knp.doctrine_behaviors.blameable_subscriber.blameable_trait: Knp\DoctrineBehaviors\Model\Blameable\Blameable
knp.doctrine_behaviors.blameable_subscriber.user_callable.class: Knp\DoctrineBehaviors\ORM\Blameable\UserCallable
Expand Down Expand Up @@ -76,6 +77,7 @@ services:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
- "%knp.doctrine_behaviors.reflection.is_recursive%"
- "%knp.doctrine_behaviors.timestampable_subscriber.timestampable_trait%"
- "%knp.doctrine_behaviors.timestampable_subscriber.db_field_type%"
tags:
- { name: doctrine.event_subscriber }

Expand Down
6 changes: 4 additions & 2 deletions src/ORM/Timestampable/TimestampableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
class TimestampableSubscriber extends AbstractSubscriber
{
private $timestampableTrait;
private $dbFieldType;

public function __construct(ClassAnalyzer $classAnalyzer, $isRecursive, $timestampableTrait)
public function __construct(ClassAnalyzer $classAnalyzer, $isRecursive, $timestampableTrait, $dbFieldType)
{
parent::__construct($classAnalyzer, $isRecursive);

$this->timestampableTrait = $timestampableTrait;
$this->dbFieldType = $dbFieldType;
}

public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
Expand All @@ -53,7 +55,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
if (!$classMetadata->hasField($field)) {
$classMetadata->mapField(array(
'fieldName' => $field,
'type' => 'datetime',
'type' => $this->dbFieldType,
'nullable' => true
));
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Knp/DoctrineBehaviors/ORM/TimestampableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ protected function getEventManager()
new \Knp\DoctrineBehaviors\ORM\Timestampable\TimestampableSubscriber(
new ClassAnalyzer(),
false,
'Knp\DoctrineBehaviors\Model\Timestampable\Timestampable'
'Knp\DoctrineBehaviors\Model\Timestampable\Timestampable',
'datetime'
));

return $em;
Expand Down

0 comments on commit 328c0ec

Please sign in to comment.