### Install and Register Bundle
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Use Composer to install the bundle and register it in the Symfony kernel configuration.
```bash
# Install with Composer (Symfony Flex auto-configures the bundle)
composer require stof/doctrine-extensions-bundle
```
```php
// For non-Flex projects, register the bundle in config/bundles.php
return [
// ...
Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle::class => ['all' => true],
];
```
--------------------------------
### Enable Stof Doctrine Extensions Bundle in AppKernel.php
Source: https://github.com/stof/stofdoctrineextensionsbundle/blob/main/docs/installation.rst
Add this line to your app/AppKernel.php file to enable the bundle after installation.
```php
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
);
// ...
}
// ...
}
```
--------------------------------
### Install Stof Doctrine Extensions Bundle with Symfony Flex
Source: https://github.com/stof/stofdoctrineextensionsbundle/blob/main/docs/installation.rst
Use this command to automatically download, register, and configure the bundle if your project uses Symfony Flex.
```bash
$ composer require stof/doctrine-extensions-bundle
```
--------------------------------
### Configure Uploadable Extension
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Configure the default path for uploads and specify custom classes for MIME type guessing and file info. This setup is for the ORM.
```yaml
stof_doctrine_extensions:
uploadable:
default_file_path: "%kernel.project_dir%/public/uploads"
mime_type_guesser_class: Stof\DoctrineExtensionsBundle\Uploadable\MimeTypeGuesserAdapter
default_file_info_class: Stof\DoctrineExtensionsBundle\Uploadable\UploadedFileInfo
orm:
default:
uploadable: true
```
--------------------------------
### Configure Stof Doctrine Extensions Bundle (XML)
Source: https://github.com/stof/stofdoctrineextensionsbundle/blob/main/docs/configuration.rst
Configure the bundle's default locale and activate ORM/MongoDB entity managers using XML configuration. This setup is an alternative to the YAML configuration.
```xml
```
--------------------------------
### Implement SoftDeleteable Entity
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Use the SoftDeleteable trait and annotation to enable soft deletion. Includes examples for removing, flushing, and toggling the filter.
```php
id; }
public function getContent(): string { return $this->content; }
public function setContent(string $content): self { $this->content = $content; return $this; }
}
// Usage: Soft delete and restore
$comment = $entityManager->find(Comment::class, 1);
$entityManager->remove($comment); // Sets deletedAt, doesn't actually delete
$entityManager->flush();
// To see deleted items, disable the filter
$entityManager->getFilters()->disable('softdeleteable');
$allComments = $entityManager->getRepository(Comment::class)->findAll(); // Includes soft-deleted
// Re-enable filter
$entityManager->getFilters()->enable('softdeleteable');
```
--------------------------------
### Build and Query a Menu Tree
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Demonstrates how to create and persist menu items to build a tree structure, and then query the hierarchy. Ensure the entity manager and repository are correctly initialized.
```php
// Usage: Build a menu tree
$repo = $entityManager->getRepository(MenuItem::class);
$home = new MenuItem();
$home->setTitle('Home');
$products = new MenuItem();
$products->setTitle('Products');
$products->setParent($home);
$electronics = new MenuItem();
$electronics->setTitle('Electronics');
$electronics->setParent($products);
$entityManager->persist($home);
$entityManager->persist($products);
$entityManager->persist($electronics);
$entityManager->flush();
// Query the tree
$tree = $repo->childrenHierarchy($home);
```
--------------------------------
### Retrieve and Revert Entity Versions
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Shows how to fetch the version history of a product and revert it to a specific version using the LogEntry repository. Requires the entity manager and LogEntry repository to be available.
```php
// Usage: Retrieve version history
use Gedmo\Loggable\Entity\LogEntry;
$product = $entityManager->find(Product::class, 1);
$logRepo = $entityManager->getRepository(LogEntry::class);
// Get all log entries for this product
$logs = $logRepo->getLogEntries($product);
// Revert to a previous version
$logRepo->revert($product, 2); // Revert to version 2
$entityManager->persist($product);
$entityManager->flush();
```
--------------------------------
### Configure Stof Doctrine Extensions Bundle (YAML)
Source: https://github.com/stof/stofdoctrineextensionsbundle/blob/main/docs/configuration.rst
Configure the bundle's default locale and activate ORM/MongoDB entity managers. The Uploadable extension can be configured with default file paths and custom classes.
```yaml
stof_doctrine_extensions:
default_locale: en_US
# Only used if you activated the Uploadable extension
uploadable:
# Default file path: This is one of the three ways you can configure the path for the Uploadable extension
default_file_path: "%kernel.project_dir%/public/uploads"
# Mime type guesser class: Optional. By default, we provide an adapter for the one present in the Mime component of Symfony
mime_type_guesser_class: Stof\DoctrineExtensionsBundle\Uploadable\MimeTypeGuesserAdapter
# Default file info class implementing FileInfoInterface: Optional. By default we provide a class which is prepared to receive an UploadedFile instance.
default_file_info_class: Stof\DoctrineExtensionsBundle\Uploadable\UploadedFileInfo
orm:
default: ~
mongodb:
default: ~
```
--------------------------------
### Configure Extensions
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Enable specific Gedmo extensions for your entity managers in the bundle configuration file.
```yaml
# config/packages/stof_doctrine_extensions.yaml
stof_doctrine_extensions:
default_locale: en_US
translation_fallback: false
persist_default_translation: false
skip_translation_on_load: false
orm:
default:
timestampable: true
sluggable: true
tree: true
translatable: true
blameable: true
loggable: true
softdeleteable: true
sortable: true
uploadable: true
```
--------------------------------
### Implement Blameable Extension in PHP
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Use the #[Gedmo\Blameable] attribute to track user creation and modification timestamps or references.
```php
id; }
public function getName(): string { return $this->name; }
public function setName(string $name): self { $this->name = $name; return $this; }
public function getCreatedBy(): ?User { return $this->createdBy; }
public function getUpdatedBy(): ?User { return $this->updatedBy; }
```
--------------------------------
### Handle File Upload in Controller
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Demonstrates how to handle file uploads in a controller using Symfony's form component and Stof's UploadableManager to persist the uploaded file with the entity.
```php
createFormBuilder($document)
->add('name')
->add('file', FileType::class)
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$uploadedFile = $form->get('file')->getData();
$em->persist($document);
$uploadableManager->markEntityToUpload($document, $uploadedFile);
$em->flush();
return $this->redirectToRoute('document_list');
}
return $this->render('document/upload.html.twig', ['form' => $form]);
}
}
```
--------------------------------
### Implement Translatable Extension in PHP
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Use the #[Gedmo\Translatable] attribute and Translatable interface to manage multi-language entity content.
```php
id; }
public function getName(): string { return $this->name; }
public function setName(string $name): self { $this->name = $name; return $this; }
public function getDescription(): ?string { return $this->description; }
public function setDescription(?string $description): self { $this->description = $description; return $this; }
public function setTranslatableLocale(string $locale): self { $this->locale = $locale; return $this; }
}
// Usage: Create category in English, then add French translation
$category = new Category();
$category->setName('Electronics');
$category->setDescription('Electronic devices and accessories');
$entityManager->persist($category);
$entityManager->flush();
// Add French translation
$category->setTranslatableLocale('fr');
$category->setName('Électronique');
$category->setDescription('Appareils électroniques et accessoires');
$entityManager->persist($category);
$entityManager->flush();
```
--------------------------------
### Implement Sluggable Extension in PHP
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Use the #[Gedmo\Slug] attribute to automatically generate URL-friendly slugs from entity fields.
```php
id; }
public function getTitle(): string { return $this->title; }
public function setTitle(string $title): self { $this->title = $title; return $this; }
public function getSlug(): string { return $this->slug; }
}
// Usage in controller
$post = new Post();
$post->setTitle('Hello World Article');
$entityManager->persist($post);
$entityManager->flush();
// $post->getSlug() returns 'hello-world-article'
```
--------------------------------
### Configure Custom Listener Classes
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Allows overriding default Gedmo listeners with custom implementations by specifying the custom class path in the configuration. Use `~` to fall back to the default listener.
```yaml
# config/packages/stof_doctrine_extensions.yaml
stof_doctrine_extensions:
class:
timestampable: App\Listener\CustomTimestampableListener
blameable: App\Listener\CustomBlameableListener
sluggable: ~ # null means use default
```
--------------------------------
### Implement Sortable Entity
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Use SortablePosition and SortableGroup annotations to manage entity ordering within specific groups.
```php
id; }
public function getName(): string { return $this->name; }
public function setName(string $name): self { $this->name = $name; return $this; }
public function getPosition(): int { return $this->position; }
public function setPosition(int $position): self { $this->position = $position; return $this; }
public function getProject(): ?Project { return $this->project; }
public function setProject(?Project $project): self { $this->project = $project; return $this; }
}
```
--------------------------------
### Configure Doctrine Extensions per Entity Manager
Source: https://github.com/stof/stofdoctrineextensionsbundle/blob/main/docs/configuration.rst
Define which extensions are enabled for specific entity managers using YAML or XML configuration files.
```yaml
# app/config/config.yml
# (or config/packages/stof_doctrine_extensions.yaml)
stof_doctrine_extensions:
default_locale: en_US
orm:
default:
tree: true
timestampable: false # not needed: listeners are not enabled by default
translatable: false
blameable: false
sluggable: false
loggable: false
ip_traceable: false
sortable: false
softdeleteable: false
uploadable: false
reference_integrity: false
other:
timestampable: true
```
```xml
```
--------------------------------
### Implement Timestampable Extension
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Apply the Timestampable attribute to entity properties to automatically track creation and update times.
```php
id; }
public function getTitle(): string { return $this->title; }
public function setTitle(string $title): self { $this->title = $title; return $this; }
public function getCreatedAt(): \DateTimeImmutable { return $this->createdAt; }
public function getUpdatedAt(): \DateTimeImmutable { return $this->updatedAt; }
}
```
--------------------------------
### Handle file uploads with Uploadable extension
Source: https://github.com/stof/stofdoctrineextensionsbundle/blob/main/docs/uploadable-extension.rst
Use the uploadable manager to mark an entity for upload after form validation. Ensure the entity property returns an UploadedFile instance.
```php
$document = new Document();
$form = $this->createFormBuilder($document)
->add('name')
->add('myFile')
->getForm()
;
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($document);
$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
// Here, "getMyFile" returns the "UploadedFile" instance that the form bound in your $myFile property
$uploadableManager->markEntityToUpload($document, $document->getMyFile());
$em->flush();
return $this->redirect($this->generateUrl('...'));
}
return $this->render('...', array('form' => $form->createView()));
```
--------------------------------
### Register Extension Mappings in Doctrine
Source: https://github.com/stof/stofdoctrineextensionsbundle/blob/main/docs/configuration.rst
Add these mappings to your Doctrine configuration to enable extensions that use their own entities. Ensure the paths point to the correct extension entity directories.
```yaml
doctrine:
orm:
entity_managers:
default:
mappings:
gedmo_translatable:
type: attribute
prefix: Gedmo\Translatable\Entity
dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Translatable/Entity"
alias: GedmoTranslatable # (optional) it will default to the name set for the mapping
is_bundle: false
gedmo_translator:
type: attribute
prefix: Gedmo\Translator\Entity
dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Translator/Entity"
alias: GedmoTranslator # (optional) it will default to the name set for the mapping
is_bundle: false
gedmo_loggable:
type: attribute
prefix: Gedmo\Loggable\Entity
dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Loggable/Entity"
alias: GedmoLoggable # (optional) it will default to the name set for the mapping
is_bundle: false
gedmo_tree:
type: attribute
prefix: Gedmo\Tree\Entity
dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Tree/Entity"
alias: GedmoTree # (optional) it will default to the name set for the mapping
is_bundle: false
```
--------------------------------
### Define Loggable Entity
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Defines a product entity that can be tracked for changes using Gedmo's Loggable extension. Ensure the entity implements the Loggable interface and uses the Gedmo\Loggable annotation.
```php
id; }
public function getName(): string { return $this->name; }
public function setName(string $name): self { $this->name = $name; return $this; }
public function getPrice(): string { return $this->price; }
public function setPrice(string $price): self { $this->price = $price; return $this; }
}
```
--------------------------------
### Register Gedmo Entity Mappings
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Configure Doctrine to recognize entity mappings for extensions that require their own database tables.
```yaml
# config/packages/doctrine.yaml
doctrine:
orm:
entity_managers:
default:
mappings:
gedmo_translatable:
type: attribute
prefix: Gedmo\Translatable\Entity
dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Translatable/Entity"
alias: GedmoTranslatable
is_bundle: false
gedmo_loggable:
type: attribute
prefix: Gedmo\Loggable\Entity
dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Loggable/Entity"
alias: GedmoLoggable
is_bundle: false
gedmo_tree:
type: attribute
prefix: Gedmo\Tree\Entity
dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Tree/Entity"
alias: GedmoTree
is_bundle: false
```
--------------------------------
### Implement Custom Timestampable Listener
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Extends the default Gedmo TimestampableListener to provide custom date handling logic, such as setting a specific timezone for timestamps.
```php
```
--------------------------------
### Enable SoftDeleteable Filter in YAML
Source: https://github.com/stof/stofdoctrineextensionsbundle/blob/main/docs/softdeleteable-filter.rst
Configure the SoftDeleteable filter within the Doctrine ORM settings to enable the behavior.
```yaml
# app/config/config.yml
# (or config/packages/doctrine.yaml if you use Flex)
doctrine:
orm:
entity_managers:
default:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
```
--------------------------------
### Configure MongoDB ODM Extensions
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Enables various Gedmo extensions for MongoDB document managers, including timestampable, sluggable, translatable, blameable, loggable, softdeleteable, and sortable.
```yaml
# config/packages/stof_doctrine_extensions.yaml
stof_doctrine_extensions:
default_locale: en_US
mongodb:
default:
timestampable: true
sluggable: true
translatable: true
blameable: true
loggable: true
softdeleteable: true
sortable: true
```
--------------------------------
### Define Uploadable Entity
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Defines an entity with Gedmo annotations for uploadable fields, specifying path generation, filename generation, and overwrite behavior. Includes getters and setters for document properties.
```php
id; }
public function getName(): string { return $this->name; }
public function setName(string $name): self { $this->name = $name; return $this; }
public function getFilePath(): ?string { return $this->filePath; }
public function getFileName(): ?string { return $this->fileName; }
public function getFileMimeType(): ?string { return $this->fileMimeType; }
public function getFileSize(): ?int { return $this->fileSize; }
}
```
--------------------------------
### Define Nested Tree Entity
Source: https://context7.com/stof/stofdoctrineextensionsbundle/llms.txt
Defines an entity for a nested tree structure using Gedmo's Tree annotations. Requires setting up the repository class and tree type.
```php
'ASC'])]
private Collection $children;
public function __construct() { $this->children = new ArrayCollection(); }
public function getId(): ?int { return $this->id; }
public function getTitle(): string { return $this->title; }
public function setTitle(string $title): self { $this->title = $title; return $this; }
public function getParent(): ?MenuItem { return $this->parent; }
public function setParent(?MenuItem $parent): self { $this->parent = $parent; return $this; }
public function getLevel(): ?int { return $this->level; }
public function getChildren(): Collection { return $this->children; }
}
```
--------------------------------
### Disable SoftDeleteable Filter Programmatically
Source: https://github.com/stof/stofdoctrineextensionsbundle/blob/main/docs/softdeleteable-filter.rst
Disable the filter at runtime to allow access to deleted entities, such as in administrative views.
```php
$filters = $em->getFilters();
$filters->disable('softdeleteable');
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.