danidelalin/doctrine-mongodb-admin-bundle
Installation
Add the bundle to your composer.json:
composer require danidelalin/doctrine-mongodb-admin-bundle
Enable it in config/bundles.php:
return [
// ...
Danidelalin\DoctrineMongoDBAdminBundle\DanidelalinDoctrineMongoDBAdminBundle::class => ['all' => true],
];
Configure Doctrine MongoDB ODM
Ensure your config/packages/doctrine_mongodb.yaml is properly set up:
doctrine_mongodb:
connections:
default:
server: '%env(MONGODB_URL)%'
options: {}
document_managers:
default:
auto_mapping: true
mappings:
App:
is_bundle: false
type: attribute
dir: '%kernel.project_dir%/src/Document'
prefix: 'App\Document'
alias: App
Create a Basic Admin Class
Extend Sonata\AdminBundle\Admin\AbstractAdmin and annotate your document:
use Danidelalin\DoctrineMongoDBAdminBundle\Admin\AbstractMongoDBAdmin;
use App\Document\User;
class UserAdmin extends AbstractMongoDBAdmin
{
protected $documentClass = User::class;
protected $translationDomain = 'App';
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('username')
->add('email');
}
}
Register the Admin
Add the admin to config/packages/sonata_admin.yaml:
sonata_admin:
options:
document_class: App\Document\User
managers:
app:
list: [App\Admin\UserAdmin]
Route the Admin
Ensure routing is enabled in config/routes.yaml:
sonata_admin:
resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
prefix: /admin
First Use Case
Clear cache and visit /admin/app/user to see your MongoDB-backed CRUD interface.
Customizing List Views
Override configureListFields to tailor the admin panel:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('id')
->add('createdAt', null, [
'template' => 'SonataAdminBundle:CRUD:base_date_field.html.twig',
]);
}
Form Customization
Use configureFormFields to modify the edit/create forms:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->tab('General')
->with('Basic Info')
->add('username', 'text')
->add('email', 'email')
->end()
->end();
}
Filtering and Sorting
Extend configureDatagridFilters and configureDatagridValues:
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('username')
->add('email');
}
Batch Actions
Enable batch operations in configureBatchActions:
protected function configureBatchActions(BatchActionMapper $batchActionMapper)
{
$batchActionMapper
->add('delete', null, [
'label' => 'Delete',
'ask_confirmation' => true,
]);
}
Integration with Sonata Blocks
Use the sonata.block service to embed admin content in blocks:
{{ render(controller('SonataAdminBundle:Block:block', {
'type': 'sonata.admin.block.admin_list',
'code': 'app_user',
'mode': 'list'
})) }}
Handling Embedded Documents
For nested documents, use SonataAdminBundle's sonata_type_model:
$formMapper->add('address', 'sonata_type_model', [
'btn_add' => 'Add Address',
'btn_edit' => 'Edit Address',
'btn_delete' => 'Delete Address',
]);
Documentation Quirks
AbstractMongoDBAdmin may differ slightly from SonataAdminBundle's AbstractAdmin. Check inheritance chains carefully.Caching Issues
php bin/console cache:clear
php bin/console doctrine:mongodb:cache:clear-metadata
--no-debug in production to avoid cache inconsistencies.MongoDB Schema Mismatches
@Document, @Field) align with the admin's expectations. Test with a fresh DB if fields disappear or errors occur.Translation Problems
translationDomain must match your translation files (e.g., messages.en.yml). Verify domain names in configureTitleField and configureListFields.Route Conflicts
sonata_admin.yaml if needed:
sonata_admin:
options:
base_route_pattern: 'custom_admin'
base_route: 'custom_admin'
Performance with Large Datasets
configureDatagridValues:
$datagridMapper->add('id', null, [], 'string', 1); // Sort by ID
$datagridMapper->add('username', null, [], 'string', 2); // Secondary sort
Enable Debug Mode
Set debug: true in config/packages/dev/sonata_admin.yaml to see SQL-like MongoDB queries.
Log Queries
Add this to config/packages/dev/doctrine_mongodb.yaml:
doctrine_mongodb:
logging: true
Check logs in var/log/dev.log.
Check Admin Registration If the admin doesn’t appear, verify:
composer dump-autoload).sonata_admin.yaml under the correct key (app in the example).Override Templates
Copy templates from vendor/sonata-project/doctrine-mongodb-admin-bundle/Resources/views/ to templates/SonataAdminBundle/ to customize without overriding core files.
Custom Field Types
Extend Sonata\AdminBundle\Form\Type\ModelType for custom MongoDB field rendering.
Event Listeners
Use Symfony events (e.g., sonata.admin.pre_persist) to modify documents before save:
$event->getSubject()->setUpdatedAt(new \DateTime());
Dynamic Admins
Generate admin classes dynamically using Sonata\AdminBundle\Generator\Generator:
$generator = $this->get('sonata.admin.generator.admin');
$generator->generate('App\Document\User', 'App\Admin\UserAdmin');
MongoDB-Specific Features
Leverage ODM features like @Index or @ReferenceMany in your documents and reflect them in the admin:
// In your document
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Index(name="username_idx", keys={"username"="text"})
*/
private $username;
How can I help you explore Laravel packages today?