Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add several missing subscribers #138

Merged
merged 7 commits into from
Oct 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ class Category
```


NB If you generate the entity with `./console doctrine:generate:entity`, you have to remove the private $id and the `setId` and `getId` functions.

After updating the database, ie. with `./console doctrine:schema:update --force`, you can now work on translations using `translate` or `getTranslations` methods.

``` php
Expand Down
2 changes: 2 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Most occurences of "listener" have been replaced with "subscriber" to honor a
subtle difference between the two that is explained
[in the documentation](http://doctrine-orm.readthedocs.org/en/latest/reference/events.html#listening-and-subscribing-to-lifecycle-events).
If you use translatable, you should map your translation classes `id` fields,
the `id` mapping is deprecated and will be removed in version 2.0
If you decided to create your own subscriber by extending a class from this library,
you might have to refactor your code because of this.
Likewise, DI parameters (for class names), and service ids have changed.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "1.1.x-dev"
}
}
}
24 changes: 24 additions & 0 deletions config/orm-services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ parameters:
knp.doctrine_behaviors.geocodable_subscriber.geocodable_trait: Knp\DoctrineBehaviors\Model\Geocodable\Geocodable
knp.doctrine_behaviors.sluggable_subscriber.class: Knp\DoctrineBehaviors\ORM\Sluggable\SluggableSubscriber
knp.doctrine_behaviors.sluggable_subscriber.sluggable_trait: Knp\DoctrineBehaviors\Model\Sluggable\Sluggable
knp.doctrine_behaviors.tree_subscriber.class: Knp\DoctrineBehaviors\ORM\Tree\TreeSubscriber
knp.doctrine_behaviors.tree_subscriber.tree_trait: Knp\DoctrineBehaviors\ORM\Tree\Tree
knp.doctrine_behaviors.sortable_subscriber.class: Knp\DoctrineBehaviors\ORM\Sortable\SortableSubscriber
knp.doctrine_behaviors.sortable_subscriber.sortable_trait: Knp\DoctrineBehaviors\ORM\Sortable\Sortable

services:
knp.doctrine_behaviors.reflection.class_analyzer:
Expand Down Expand Up @@ -67,6 +71,26 @@ services:
tags:
- { name: doctrine.event_subscriber }

knp.doctrine_behaviors.tree_subscriber:
class: "%knp.doctrine_behaviors.tree_subscriber.class%"
public: false
arguments:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
- "%knp.doctrine_behaviors.reflection.is_recursive%"
- "%knp.doctrine_behaviors.tree_subscriber.tree_trait%"
tags:
- { name: doctrine.event_subscriber }

knp.doctrine_behaviors.sortable_subscriber:
class: "%knp.doctrine_behaviors.sortable_subscriber.class%"
public: false
arguments:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
- "%knp.doctrine_behaviors.reflection.is_recursive%"
- "%knp.doctrine_behaviors.sortable_subscriber.sortable_trait%"
tags:
- { name: doctrine.event_subscriber }

knp.doctrine_behaviors.blameable_subscriber:
class: "%knp.doctrine_behaviors.blameable_subscriber.class%"
arguments:
Expand Down
3 changes: 0 additions & 3 deletions src/Model/Sortable/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

trait Sortable
{
/**
* @ORM\Column(type="integer")
*/
protected $sort = 1;

private $reordered = false;
Expand Down
9 changes: 0 additions & 9 deletions src/Model/Translatable/TranslationMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@
*/
trait TranslationMethods
{
/**
* Returns translation ID.
*
* @return integer The ID.
*/
public function getId()
{
return $this->id;
}

/**
* Sets entity, that this translation should be mapped to.
Expand Down
9 changes: 0 additions & 9 deletions src/Model/Translatable/TranslationProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@
*/
trait TranslationProperties
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\Column(type="string")
*/
protected $locale;

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Model/Tree/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ trait Node
*/
private $parentNode;

/**
* @var string $materializedPath
*
* @ORM\Column(type="string", length=255)
*/
protected $materializedPath = '';

/**
Expand Down
77 changes: 77 additions & 0 deletions src/ORM/Sortable/SortableSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the KnpDoctrineBehaviors package.
*
* (c) KnpLabs <http://knplabs.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Knp\DoctrineBehaviors\ORM\Sortable;

use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;

use Knp\DoctrineBehaviors\ORM\AbstractSubscriber;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs,
Doctrine\ORM\Events,
Doctrine\ORM\Mapping\ClassMetadata;

/**
* Sortable subscriber.
*
* Adds mapping to the sortable entities.
*/
class SortableSubscriber extends AbstractSubscriber
{
private $sortableTrait;

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

$this->sortableTrait = $sortableTrait;
}

public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
$classMetadata = $eventArgs->getClassMetadata();

if (null === $classMetadata->reflClass) {
return;
}

if ($this->isSortable($classMetadata)) {

if (!$classMetadata->hasField('sort')) {
$classMetadata->mapField(array(
'fieldName' => 'sort',
'type' => 'integer'
));
}
}
}

public function getSubscribedEvents()
{
return [Events::loadClassMetadata];
}

/**
* Checks if entity is a sortable
*
* @param ClassMetadata $classMetadata The metadata
*
* @return Boolean
*/
private function isSortable(ClassMetadata $classMetadata)
{
return $this->getClassAnalyzer()->hasTrait(
$classMetadata->reflClass,
$this->sortableTrait,
$this->isRecursive
);
}
}
134 changes: 134 additions & 0 deletions src/ORM/Translatable/TranslatableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;

use Knp\DoctrineBehaviors\ORM\AbstractSubscriber;
use Doctrine\ORM\ORMException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Id\BigIntegerIdentityGenerator;
use Doctrine\ORM\Id\IdentityGenerator;

use Doctrine\Common\EventSubscriber,
Doctrine\ORM\Mapping\ClassMetadata,
Expand Down Expand Up @@ -66,6 +71,127 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)

if ($this->isTranslation($classMetadata)) {
$this->mapTranslation($classMetadata);
$this->mapId(
$classMetadata,
$eventArgs->getEntityManager()
->getConnection()
->getDatabasePlatform()
);
}
}

/**
* Kept for BC-compatibility purposes : people expect this lib to map ids for
* translations.
*
* @deprecated It should be removed because it probably does not work with
* every doctrine version.
*
* @see https://github.com/doctrine/doctrine2/blob/0bff6aadbc9f3fd8167a320d9f4f6cf269382da0/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php#L508
*/
private function mapId(ClassMetadata $class, AbstractPlatform $platform)
{
if (!$class->hasField('id')) {
$builder = new ClassMetadataBuilder($class);
$builder->createField('id', 'integer')->isPrimaryKey()->generatedValue()->build();
/// START DOCTRINE CODE
$idGenType = $class->generatorType;
if ($idGenType == ClassMetadata::GENERATOR_TYPE_AUTO) {
if ($platform->prefersSequences()) {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_SEQUENCE);
} else if ($platform->prefersIdentityColumns()) {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY);
} else {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_TABLE);
}
}

// Create & assign an appropriate ID generator instance
switch ($class->generatorType) {
case ClassMetadata::GENERATOR_TYPE_IDENTITY:
// For PostgreSQL IDENTITY (SERIAL) we need a sequence name. It defaults to
// <table>_<column>_seq in PostgreSQL for SERIAL columns.
// Not pretty but necessary and the simplest solution that currently works.
$sequenceName = null;
$fieldName = $class->identifier ? $class->getSingleIdentifierFieldName() : null;

if ($platform instanceof Platforms\PostgreSQLPlatform) {
$columnName = $class->getSingleIdentifierColumnName();
$quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);
$sequenceName = $class->getTableName() . '_' . $columnName . '_seq';
$definition = array(
'sequenceName' => $platform->fixSchemaElementName($sequenceName)
);

if ($quoted) {
$definition['quoted'] = true;
}

$sequenceName = $this->em->getConfiguration()->getQuoteStrategy()->getSequenceName($definition, $class, $platform);
}

$generator = ($fieldName && $class->fieldMappings[$fieldName]['type'] === 'bigint')
? new BigIntegerIdentityGenerator($sequenceName)
: new IdentityGenerator($sequenceName);

$class->setIdGenerator($generator);

break;

case ClassMetadata::GENERATOR_TYPE_SEQUENCE:
// If there is no sequence definition yet, create a default definition
$definition = $class->sequenceGeneratorDefinition;

if ( ! $definition) {
$fieldName = $class->getSingleIdentifierFieldName();
$columnName = $class->getSingleIdentifierColumnName();
$quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);
$sequenceName = $class->getTableName() . '_' . $columnName . '_seq';
$definition = array(
'sequenceName' => $platform->fixSchemaElementName($sequenceName),
'allocationSize' => 1,
'initialValue' => 1,
);

if ($quoted) {
$definition['quoted'] = true;
}

$class->setSequenceGeneratorDefinition($definition);
}

$sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator(
$this->em->getConfiguration()->getQuoteStrategy()->getSequenceName($definition, $class, $platform),
$definition['allocationSize']
);
$class->setIdGenerator($sequenceGenerator);
break;

case ClassMetadata::GENERATOR_TYPE_NONE:
$class->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
break;

case ClassMetadata::GENERATOR_TYPE_UUID:
$class->setIdGenerator(new \Doctrine\ORM\Id\UuidGenerator());
break;

case ClassMetadata::GENERATOR_TYPE_TABLE:
throw new ORMException("TableGenerator not yet implemented.");
break;

case ClassMetadata::GENERATOR_TYPE_CUSTOM:
$definition = $class->customGeneratorDefinition;
if ( ! class_exists($definition['class'])) {
throw new ORMException("Can't instantiate custom generator : " .
$definition['class']);
}
$class->setIdGenerator(new $definition['class']);
break;

default:
throw new ORMException("Unknown generator type: " . $class->generatorType);
}
/// END DOCTRINE COPY / PASTED code
}
}

Expand Down Expand Up @@ -109,6 +235,14 @@ private function mapTranslation(ClassMetadata $classMetadata)
]],
]);
}

if (!$classMetadata->hasField('locale')) {
$classMetadata->mapField(array(
'fieldName' => 'locale',
'type' => 'string'
));
}

}

/**
Expand Down
Loading