mitchellvanw / laravel-doctrine

NO LONGER MAINTAINED! A Doctrine 2 implementation that melts with Laravel
MIT License
187 stars 74 forks source link

Add In Support For Doctrine Extensions #59

Closed yooper closed 7 years ago

yooper commented 10 years ago

Hello,

Thanks for developing this package. It would be nice to add in support for Doctrine Extension into this package. I was thinking the main composer file would add in the Doctrine Extensions. But the configuration and inclusion would be set via this package's config file.

Example, in the file app/config/packages/mitchellvanw/laravel-doctrine/doctrine.php have a config array

['extension' => ['\Gedmo\Tree\TreeListener', 'Gedmo\Loggable\LoggableListener' ]]
nWidart commented 9 years ago

This would be very helpful! In the mean time, how would go about using the Doctrine Extensions inside Laravel ? I've required the package, "gedmo/doctrine-extensions": "2.3.9" but when adding the @Gedmo\Translatable annotation, when updating the schema it says:

The annotation "@Gedmo\Mapping\Annotation\Translatable" in property Modules\Blog\Entities\Post::$title does not exist, or could not be auto-loaded.`

Any hints on how to use the extensions would be very useful!

Thank you,

zhouyixiang commented 9 years ago

@nWidart I have the same issue, have you found the solution?

nWidart commented 9 years ago

I've sadly not found a way to make it work.

In the mean time I've made a trait, a refactored version of @freekmurze, that handles the translations of an entity.

It basically lets you do something like the following:

$category = $this->entityManager->getRepository('Modules\Blog\Entities\Category')->find(1);
$post = new Post();
$post->createOrUpdateTranslation('title', 'en', 'Title en');
$post->createOrUpdateTranslation('title', 'fr', 'Titre fr');
$post->setCategory($category);

$this->entityManager->persist($post);
$this->entityManager->flush();

And to read a translation, simple access the property like usual:

$post->title

This solution isn't as clean as the Doctrine Extensions, but hopefully someone figures out how to use them inside Laravel with this package.

guiwoda commented 9 years ago

+1 to this, I think it needs a more flexible EM binding. Maybe pre- and post- EM created events to hook up extensions?

patrickbrouwers commented 9 years ago

I got it working using: (In an own ServiceProvider)

$entityManager = $this->app['Doctrine\ORM\EntityManager'];
$eventManager = $entityManager->getEventManager();
$reader = $entityManager->getConfiguration()->getMetadataDriverImpl()->getReader();

DoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
    new MappingDriverChain(),
    $reader
);

$sluggableListener = new SluggableListener();
$sluggableListener->setAnnotationReader($reader);
$eventManager->addEventSubscriber($sluggableListener);

$sortableListener = new SortableListener;
$sortableListener->setAnnotationReader($reader);
$eventManager->addEventSubscriber($sortableListener);
msdm commented 9 years ago

The code provided by @patrickbrouwers above works fine for the annotations but it does not create the database tables needed for some of the extensions such as Loggable and Translatable. The following code fixes this issue.

$entityManager = $this->app['Doctrine\ORM\EntityManager'];
$eventManager = $entityManager->getEventManager();
$reader = $entityManager->getConfiguration()->getMetadataDriverImpl()->getReader();
$driverChain = new MappingDriverChain();

DoctrineExtensions::registerMappingIntoDriverChainORM($driverChain, $reader);

$driverChain->addDriver($entityManager->getConfiguration()->getMetadataDriverImpl(), 'App');
$entityManager->getConfiguration()->setMetadataDriverImpl($driverChain);

$loggableListener = new LoggableListener();
$loggableListener->setAnnotationReader($reader);
$eventManager->addEventSubscriber($loggableListener);
patrickbrouwers commented 9 years ago

Nice inprovement. Didn't try those onces yet.

patrickbrouwers commented 9 years ago

In the meantime I have open-sourced my Laravel Doctrine solution, in which enabling DoctrineExtensions is as easy as adding the extension to the config file.

https://github.com/patrickbrouwers/Laravel-Doctrine

torniker commented 9 years ago

+1

mitchellvanw commented 7 years ago

Closing because project is no longer maintained. Thanks.