ebidtech/doctrine-extensions-bundle
Installation Add the bundle via Composer:
composer require ebidtech/doctrine-extensions-bundle
Register the bundle in config/bundles.php:
return [
// ...
Ebidtech\DoctrineExtensionsBundle\EbidtechDoctrineExtensionsBundle::class => ['all' => true],
];
Enable Extensions
Configure Doctrine extensions in config/packages/doctrine.yaml:
doctrine:
orm:
mappings:
gedmo_loggable: ~
gedmo_timestampable: ~
gedmo_translatable: ~
# Add other extensions as needed (e.g., `gedmo_tree`, `gedmo_sortable`)
First Use Case: Timestampable Entities
Apply the Timestampable trait to an entity (e.g., Post):
use Gedmo\Mapping\Annotation as Gedmo;
class Post
{
/**
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* @Gedmo\Timestampable(on="update")
*/
private $updatedAt;
}
Run migrations (php bin/console doctrine:migrations:diff + php bin/console doctrine:migrations:migrate).
Logging Entity Changes
Use Loggable to track modifications:
use Gedmo\Mapping\Annotation as Gedmo;
class Product
{
/**
* @Gedmo\Loggable
*/
private $name;
}
config/packages/doctrine_extensions.yaml:
ebidtech_doctrine_extensions:
loggable: true
Translatable Entities For multilingual support:
use Gedmo\Mapping\Annotation as Gedmo;
class Article
{
/**
* @Gedmo\TranslationEntity(class="App\Entity\ArticleTranslation")
*/
private $title;
}
ArticleTranslation) with locale and field fields.Tree Structures
Use Tree for hierarchical data (e.g., categories):
use Gedmo\Mapping\Annotation as Gedmo;
class Category
{
/**
* @Gedmo\Tree(type="nested")
*/
private $parent;
}
php bin/console doctrine:query:sql "SELECT * FROM category_tree".Sortable Lists For ordered lists (e.g., menus):
use Gedmo\Mapping\Annotation as Gedmo;
class MenuItem
{
/**
* @Gedmo\SortablePosition
*/
private $position;
}
prePersist, preUpdate, etc. Override these if custom logic is needed.Timestampable) by adding your own logic in postLoad/preFlush events.GEDMO_SLUG or GEDMO_TRANSLATE in repositories:
$query->addSelect('GEDMO_TRANSLATE(e.title, :locale) as translatedTitle');
Outdated Package
doctrine/doctrine-extensions) manually.Missing Configuration
Loggable require a log_entry table. Run migrations after enabling:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Circular References in Trees
Tree extension throws errors if parent-child cycles exist. Validate with:
$category->getParent()->getRoot(); // Check for cycles
Translation Locale Handling
locale column in the translation table. Ensure it’s set in ArticleTranslation:
/**
* @ORM\Column(type="string", length=20)
*/
private $locale;
Performance with Loggable
@Gedmo\Loggable(skipOnNull=true) to optimize.config/packages/dev/doctrine.yaml:
doctrine:
dbal:
logging: true
profiling: true
php bin/console debug:event-dispatcher
Look for gedmo.* listeners.Custom Log Entry Class
Override the default LogEntry by configuring in doctrine_extensions.yaml:
ebidtech_doctrine_extensions:
loggable:
log_entry_class: App\Entity\CustomLogEntry
Slug Generation
Customize slugs by implementing Gedmo\Sluggable\SluggableListener:
$slugger->setTransliteration(true);
$slugger->setSeparator('-');
Tree Type
Switch between nested, materialized_path, or closure_table in annotations:
@Gedmo\Tree(type="materialized_path")
Timestamp Format Override default timestamps by adding a setter:
public function setCreatedAt(\DateTimeInterface $date): self
{
$this->createdAt = $date->format('Y-m-d H:i:s.u');
return $this;
}
How can I help you explore Laravel packages today?