Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
30 changes: 28 additions & 2 deletions src/Model/Behavior/SequenceBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class SequenceBehavior extends Behavior
'start' => 1,
];

/**
* Old values for the entity being deleted
*
* @var array|null
*/
protected $_oldValues;

/**
* Normalize config options.
*
Expand Down Expand Up @@ -228,21 +235,40 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
* When you delete a record from a set, you need to decrement the order of all
* records that were after it in the set.
*
* @param \Cake\Event\Event $event The beforeDelete event that was fired.
* @param \Cake\Event\Event $event The afterDelete event that was fired.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be undone.

* @param \Cake\ORM\Entity $entity The entity that is going to be saved.
*
* @return void
*/
public function beforeDelete(Event $event, Entity $entity)
{
$this->_oldValues = $this->_getOldValues($entity);
}

/**
* When you delete a record from a set, you need to decrement the order of all
* records that were after it in the set.
*
* @param \Cake\Event\Event $event The afterDelete event that was fired.
* @param \Cake\ORM\Entity $entity The entity that is going to be saved.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entity that was deleted.

*
* @return void
*/
public function afterDelete(Event $event, Entity $entity)
{
if (!$this->_oldValues) {
return;
}

$orderField = $this->_config['order'];
list($order, $scope) = $this->_getOldValues($entity);
list($order, $scope) = $this->_oldValues;

$this->_sync(
[$orderField => $this->_getUpdateExpression('-')],
[$orderField . ' >' => $order],
$scope
);
$this->_oldValues = null;
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/Fixture/UniqueItemsFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace ADmad\Sequence\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

class UniqueItemsFixture extends TestFixture
{
/**
* fields property.
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'null' => true],
'position' => ['type' => 'integer', 'null' => true],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']],
'position' => ['type' => 'unique', 'columns' => ['position']],
],
];

/**
* records property.
*
* @var array
*/
public $records = [
['name' => 'Item A', 'position' => 0],
['name' => 'Item B', 'position' => 1],
['name' => 'Item C', 'position' => 2],
['name' => 'Item D', 'position' => 3],
['name' => 'Item E', 'position' => 4],
];
}
19 changes: 19 additions & 0 deletions tests/TestCase/Model/Behavior/SequenceBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,21 @@ public function initialize(array $config)
}
}

class UniqueItems extends Table
{
public function initialize(array $config)
{
$this->addBehavior('ADmad/Sequence.Sequence', ['start' => 0]);
}
}

class SequenceTest extends TestCase
{
public $fixtures = [
'plugin.ADmad/Sequence.Items',
'plugin.ADmad/Sequence.GroupedItems',
'plugin.ADmad/Sequence.KeywordItems',
'plugin.ADmad/Sequence.UniqueItems'
];

/**
Expand Down Expand Up @@ -241,6 +250,16 @@ public function testDelete()
$this->assertOrder([1, 2, 4, 5], $GroupedItems, ['group_field' => 1]);
$this->assertOrder([6, 7, 8, 9, 10], $GroupedItems, ['group_field' => 2]);
$this->assertOrder([11, 12, 13, 14, 15], $GroupedItems, ['group_field' => 3]);

$UniqueItems = TableRegistry::get('UniqueItems', [
'table' => 'unique_items',
'alias' => 'UniqueItems',
'className' => 'ADmad\Sequence\Test\TestCase\Model\Behavior\UniqueItems',
]);

$entity = $UniqueItems->get(3);
$UniqueItems->delete($entity);
$this->assertOrder([1, 2, 4, 5], $UniqueItems);
}

/**
Expand Down