Installation
composer require devpack/gedmo-tree-recalc
(For Laravel, skip Symfony bundle registration—see "Implementation Patterns" for Laravel-specific setup.)
First Use Case Run the command to recalculate a corrupted tree structure:
php artisan gedmo:tree:recalc App\Models\YourTreeEntity
(Replace YourTreeEntity with your model using Gedmo\Tree\Treeable.)
Where to Look First
Treeable and has lft, rgt, level, and root columns.Laravel Integration
AppServiceProvider:
use DevPack\GedmoTreeRecalc\Command\RecalcTreeCommand;
public function register()
{
$this->commands([
RecalcTreeCommand::class,
]);
}
php artisan gedmo:tree:recalc App\Models\Category
Workflow for Tree Maintenance
chunk() or queueing the command.Entity Requirements
Ensure your model extends Gedmo\Tree\Treeable and includes:
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @Gedmo\TreeType
*/
protected $parent;
postPersist/postRemove if needed:
$entityManager->getEventManager()->addEventListener(
\Doctrine\ORM\Events::postRemove,
function ($event) {
$entity = $event->getObject();
if ($entity instanceof Treeable) {
$this->runRecalcCommand($entity);
}
}
);
$this->artisan('gedmo:tree:recalc', ['entity' => 'App\Models\Category'])
->expectsQuestion('Confirm?', 'yes')
->assertExitCode(0);
Column Mismatch
Column 'lft' not found or similar.lft, rgt, level columns). Run migrations if needed.Circular References
Stack overflow.Permission Issues
Command not found in Laravel.composer dump-autoload
php artisan cache:clear
--dry-run flag (if supported) to preview changes without executing.$entityManager->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
$root = $entityManager->getRepository(YourEntity::class)->findRoot();
$this->recalcCommand->recalculateSubtree($root);
Custom Recalculation Logic
Override the command’s recalculate() method to add pre/post-processing:
class CustomRecalcCommand extends RecalcTreeCommand
{
protected function recalculate(EntityManager $em, Treeable $entity)
{
// Custom logic (e.g., validate before recalculating)
parent::recalculate($em, $entity);
}
}
Queueable Recalculation Wrap the command in a job for async processing:
use DevPack\GedmoTreeRecalc\Command\RecalcTreeCommand;
class RecalculateTreeJob implements ShouldQueue
{
public function handle()
{
$this->artisan('gedmo:tree:recalc', ['entity' => 'App\Models\Category']);
}
}
Configurable Depth Limits Extend the command to limit recursion depth (e.g., for performance):
$this->artisan('gedmo:tree:recalc', [
'entity' => 'App\Models\Category',
'--max-depth' => 5, // Hypothetical flag
]);
How can I help you explore Laravel packages today?