Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/KnpLabs/DoctrineBehaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
docteurklein committed Mar 28, 2012
2 parents 56575e2 + e927cf7 commit 4936aee
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 41 deletions.
14 changes: 10 additions & 4 deletions config/orm-services.yml
Expand Up @@ -3,8 +3,8 @@ parameters:
knp.doctrine_behaviors.softdeletable_listener.class: Knp\DoctrineBehaviors\ORM\SoftDeletable\SoftDeletableListener
knp.doctrine_behaviors.timestampable_listener.class: Knp\DoctrineBehaviors\ORM\Timestampable\TimestampableListener
knp.doctrine_behaviors.blameable_listener.class: Knp\DoctrineBehaviors\ORM\Blameable\BlameableListener
knp.doctrine_behaviors.blameable_listener.user_callable: Knp\DoctrineBehaviors\ORM\Blameable\UserCallable
knp.doctrine_behaviors.user_entity: ~
knp.doctrine_behaviors.blameable_listener.user_callable.class: Knp\DoctrineBehaviors\ORM\Blameable\UserCallable
knp.doctrine_behaviors.blameable_listener.user_entity: ~

services:
knp.doctrine_behaviors.translatable_listener:
Expand All @@ -29,13 +29,19 @@ services:
class: %knp.doctrine_behaviors.blameable_listener.class%
arguments:
- @knp.doctrine_behaviors.blameable_listener.user_callable
- %knp.doctrine_behaviors.user_entity%
- %knp.doctrine_behaviors.blameable_listener.user_entity%
public: false
tags:
- { name: doctrine.event_subscriber }

knp.doctrine_behaviors.blameable_listener.user_callable:
class: %knp.doctrine_behaviors.blameable_listener.user_callable%
class: %knp.doctrine_behaviors.blameable_listener.user_callable.class%
arguments:
- @service_container # because of circular dep
public: false

knp.doctrine_behaviors.geocodable_listener:
class: %knp.doctrine_behaviors.geocodable_listener.class%
public: false
tags:
- { name: doctrine.event_subscriber }
32 changes: 14 additions & 18 deletions src/Knp/DoctrineBehaviors/ORM/Timestampable/Timestampable.php
Expand Up @@ -19,35 +19,19 @@
trait Timestampable
{
/**
* @var datetime $createdAt
* @var DateTime $createdAt
*
* @ORM\Column(type="datetime")
*/
private $createdAt;

/**
* @var datetime $updatedAt
* @var DateTime $updatedAt
*
* @ORM\Column(type="datetime")
*/
private $updatedAt;

/**
* Updates createdAt value.
*/
public function updateCreatedAt()
{
$this->createdAt = new \DateTime("now");
}

/**
* Updates updatedAt value.
*/
public function updateUpdatedAt()
{
$this->updatedAt = new \DateTime("now");
}

/**
* Returns createdAt value.
*
Expand All @@ -67,4 +51,16 @@ public function getUpdatedAt()
{
return $this->updatedAt;
}

/**
* Updates createdAt and updatedAt timestamps.
*/
public function updateTimestamps()
{
if (null === $this->createdAt) {
$this->createdAt = new \DateTime('now');
}

$this->updatedAt = new \DateTime('now');
}
}
Expand Up @@ -11,11 +11,8 @@

namespace Knp\DoctrineBehaviors\ORM\Timestampable;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;

use Doctrine\Common\EventSubscriber,
Doctrine\ORM\Event\OnFlushEventArgs,
use Doctrine\ORM\Event\LoadClassMetadataEventArgs,
Doctrine\Common\EventSubscriber,
Doctrine\ORM\Events;

/**
Expand All @@ -28,20 +25,12 @@ class TimestampableListener implements EventSubscriber
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
$classMetadata = $eventArgs->getClassMetadata();
if ($this->isEntitySupported($classMetadata)) {
$classMetadata->addLifecycleCallback('updateCreatedAt', Events::prePersist);
$classMetadata->addLifecycleCallback('updateUpdatedAt', Events::prePersist);
$classMetadata->addLifecycleCallback('updateUpdatedAt', Events::preUpdate);
if ($classMetadata->reflClass->hasMethod('updateTimestamps')) {
$classMetadata->addLifecycleCallback('updateTimestamps', Events::prePersist);
$classMetadata->addLifecycleCallback('updateTimestamps', Events::preUpdate);
}
}

private function isEntitySupported(ClassMetadata $classMetadata)
{
$traitNames = $classMetadata->reflClass->getTraitNames();

return in_array('Knp\DoctrineBehaviors\ORM\Timestampable\Timestampable', $traitNames);
}

public function getSubscribedEvents()
{
return [Events::loadClassMetadata];
Expand Down
16 changes: 13 additions & 3 deletions src/Knp/DoctrineBehaviors/ORM/Translatable/Translatable.php
Expand Up @@ -24,7 +24,7 @@ trait Translatable
* Will be mapped to translatable entity
* by TranslatableListener
*/
protected $translations;
private $translations;

/**
* Returns collection of translations.
Expand All @@ -43,8 +43,8 @@ public function getTranslations()
*/
public function addTranslation($translation)
{
$translation->setTranslatable($this);
$this->getTranslations()->add($translation);
$translation->setTranslatable($this);
}

/**
Expand All @@ -70,7 +70,7 @@ public function translate($locale)
return $translation;
}

$class = get_class($this).'Translation';
$class = self::getTranslationEntityClass();
$translation = new $class();
$translation->setLocale($locale);

Expand All @@ -79,6 +79,16 @@ public function translate($locale)
return $translation;
}

/**
* Returns translation entity class name.
*
* @return string
*/
static public function getTranslationEntityClass()
{
return __CLASS__.'Translation';
}

/**
* Finds specific translation in collection by its locale.
*
Expand Down

0 comments on commit 4936aee

Please sign in to comment.