Skip to content

Commit

Permalink
Deleting a bean in database now impacts the many to one related lists.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc TEYSSIER committed Oct 4, 2016
1 parent 9b8943c commit 2bbf8c2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/Mouf/Database/TDBM/AbstractTDBMObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ public function _setStatus($state)
foreach ($this->dbRows as $dbRow) {
$dbRow->_setStatus($state);
}

if ($state === TDBMObjectStateEnum::STATE_DELETED) {
$this->onDelete();
}
}

/**
Expand Down Expand Up @@ -631,4 +635,11 @@ public function _getCachedRelationships()
* @return string[]
*/
abstract protected function getUsedTables();

/**
* Method called when the bean is removed from database.
*/
protected function onDelete()
{
}
}
2 changes: 1 addition & 1 deletion src/Mouf/Database/TDBM/MapIterator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare (strict_types = 1);
declare(strict_types=1);

namespace Mouf\Database\TDBM;

Expand Down
29 changes: 29 additions & 0 deletions src/Mouf/Database/TDBM/Utils/BeanDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,8 @@ class $baseClassName extends $extends implements \\JsonSerializable

$str .= $this->generateGetUsedTablesCode();

$str .= $this->generateOnDeleteCode();

$str .= '}
';

Expand Down Expand Up @@ -800,4 +802,31 @@ protected function getUsedTables()
}
', $code);
}

private function generateOnDeleteCode()
{
$code = '';
$relationships = $this->getPropertiesForTable($this->table);
foreach ($relationships as $relationship) {
if ($relationship instanceof ObjectBeanPropertyDescriptor) {
$code .= sprintf(' $this->setRef('.var_export($relationship->getForeignKey()->getName(), true).', null, '.var_export($this->table->getName(), true).');');
}
}

if ($code) {
return sprintf('
/**
* Method called when the bean is removed from database.
*
*/
protected function onDelete()
{
parent::onDelete();
%s
}
', $code);
}

return '';
}
}
2 changes: 1 addition & 1 deletion tests/Mouf/Database/TDBM/AlterableResultIteratorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare (strict_types = 1);
declare(strict_types=1);

namespace Mouf\Database\TDBM;

Expand Down
27 changes: 25 additions & 2 deletions tests/Mouf/Database/TDBM/TDBMDaoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
use Mouf\Database\TDBM\Test\Dao\UserDao;
use Mouf\Database\TDBM\Utils\TDBMDaoGenerator;

/**
*/
class TDBMDaoGeneratorTest extends TDBMAbstractServiceTest
{
/** @var TDBMDaoGenerator $tdbmDaoGenerator */
Expand Down Expand Up @@ -272,6 +270,31 @@ public function testDirectReversedRelationship()
$this->assertEquals('speedy.gonzalez', $arr[1]->getLogin());
}

/**
* @depends testDaoGeneration
*/
public function testDeleteInDirectReversedRelationship()
{
$countryDao = new CountryDao($this->tdbmService);
$country = $countryDao->getById(1);

$userDao = new UserDao($this->tdbmService);
$newUser = new UserBean('John Snow', 'john@snow.com', $country, 'john.snow');
$userDao->save($newUser);

$users = $country->getUsers();

$arr = $users->toArray();

$this->assertCount(2, $arr);

$userDao->delete($arr[1]);

$users = $country->getUsers();
$arr = $users->toArray();
$this->assertCount(1, $arr);
}

/**
* @depends testDaoGeneration
*/
Expand Down

0 comments on commit 2bbf8c2

Please sign in to comment.