Skip to content

Commit

Permalink
add missing sortable mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Grégoire Paris committed Sep 25, 2014
1 parent c1ed251 commit 6842c39
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config/orm-services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ parameters:
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 @@ -79,6 +81,16 @@ services:
tags:
- { name: doctrine.event_subscriber }

knp.doctrine_behaviors.sortable:
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
77 changes: 77 additions & 0 deletions src/ORM/Tree/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
);
}
}

0 comments on commit 6842c39

Please sign in to comment.