Skip to content

Commit 4017af6

Browse files
committed
Minor improvements for Sortable Comparable
1. Switch to using Doctrine Comparable interface which is exactly the same 2. Add document for the new behavior
1 parent 5dc983f commit 4017af6

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

doc/sortable.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Content:
3030
- [Yaml](#yaml-mapping) mapping example
3131
- [Xml](#xml-mapping) mapping example
3232
- Basic usage [examples](#basic-examples)
33+
- Custom comparison method (#custom-comparisons)
3334

3435

3536
<a name="including-extension"></a>
@@ -295,3 +296,33 @@ To move an item at the end of the list, you can set the position to `-1`:
295296
```
296297
$item2->setPosition(-1);
297298
```
299+
300+
<a name="custom-comparisons"></a>
301+
302+
## Custom comparison:
303+
304+
Sortable works be comparing objects in the same group to see how they should be positioned. From time to time you may want to customize the way these
305+
objects are compared by implementing the Doctrine\Common\Comparable interface
306+
307+
``` php
308+
<?php
309+
namespace Entity;
310+
311+
use Doctrine\Common\Comparable;
312+
313+
/**
314+
* @ORM\Table(name="items")
315+
* @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
316+
*/
317+
class Item implements Comparable
318+
{
319+
public function compareTo($other)
320+
{
321+
// return 1 if this object is considered greater than the compare value
322+
323+
// return -1 if this object is considered less than the compare value
324+
325+
// return 0 if this object is considered equal to the compare value
326+
}
327+
}
328+
```

lib/Gedmo/Sortable/Comparable.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/Gedmo/Sortable/SortableListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Gedmo\Sortable;
44

5+
use Doctrine\Common\Comparable;
56
use Doctrine\Common\EventArgs;
67
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
78
use Doctrine\Common\Persistence\Proxy;

0 commit comments

Comments
 (0)