I have a Symfony2 project and today I'm asked to make it internationalized.
I have several MySQL entities and the new translated contents that I would like to put in a single table containing:
ID | language | content_key | content_translation
Once the _locale is set, I would like to be able to pass this at Entity level, in order to make the Model layer retrieve the content itselves (making a second call to the translation table if that content is one of the translatable one).
I've tried to use Translatable Extension, but since it is a pre-existing database and project, it is not an easy way to pursue.
How can I inject the request (or pass the _locale only) to my Entities?
Thanks a lot,
Andrea
I think you are using Doctrine as ORM with Symfony2?
Then have a look at the Doctrine Extensions. This bundle provides different new behaviors for your entities.
It has also a translatable behavior which is really powerfull.
Integrating the doctrine extensions in Symfony2 is not so complicated.
If you can't use the Doctrine Extensions with your Code/Database, i would try the Doctrine Event Listeners to develop the translatable behavior of your own.
Related
I am running a Symfony 3.4 based web service using Doctrine to manage and persist the different data entities.
Now I am trying to implement a method which transfers older, abandoned user accounts to another database which acts as archive.
Regarding to the Symfony docs it should be no problem to configure Doctrine to manage different database connections and entity managers.
However I do not completely understand the process on how to setup this use case:
Assume the Symfony project has different data entities DataEntity1, DataEntity2, etc. and different infrastructure entities Infrastructure1, etc..
How to tell Doctrine to initialize the archive DB with the data entities only?
How to move the entities between the DBs? Is loading them from entity manager 1 and persisting them in entity manger 2 the correct way?
Is there any best practice on how to do this?
If I understand your question correctly, you should use the prefix option for the mapping configuration.
prefix
A common namespace prefix that all entities of this mapping share.
This prefix should never conflict with prefixes of other defined
mappings otherwise some of your entities cannot be found by Doctrine.
This option defaults to the bundle namespace + Entity, for example for
an application bundle called AcmeHelloBundle prefix would be
Acme\HelloBundle\Entity.
Have a look at https://symfony.com/doc/3.4/reference/configuration/doctrine.html it shoul help you.
To move the entities between the two DBs, you should have two entity managers and use the correct one to persist olders accounts.
Hope this helps.
I'm trying to teach myself ZF2 in conjunction with Doctrine 2. I've completed both the Album tutorial and Blog Tutorial on Zend's website successfully. Now I'm trying to go back and convert the Blog Tutorial to use Doctrine 2. I believe I've successfully setup my config for doctrine and used DI to get it inside of my controller (WriteController.php) since I am able to dump the contents of it within my action. I don't get any errors so long as I don't do anything with it.
My question is what roll does Doctrine take in the Controller -> Service -> Mapper -> Backend layered structure which was taught in the Blog tutorial? (Reference To what I mean)
Also, I'm assuming Backend is referring to my Models. Is this correct?
Would I just replace any references to /Blog/Model/Post with /Blog/Entity/Blog?
The Doctrine is the Mapper. And maybe we could say also the Service (through EntityRepository). But usually you will create your own Service Layer.
The Backend is not the entities it self. Entities in one way to map the several options of backend. As backend you can understand the several options of Relational Databases (Mysql, SqlServer, Oracle, etc) NoSql Databases (like MongoDB), file system and so on.
I didn't understand your last question. But when I use Doctrine I always create my entities in /MyModule/Entity namespace. While when I use the standart Zend/Db I always create in /MyModule/Model. I do that by standardizing matters.
I am building a multi-lingual application with Zend Framework 2.
Currently, I have every object with a translatable output implement the TranslatorAwareInterface and use the TranslatorAwareTrait. So, I'am injecting a translator instance into every form, input filter and controller object in order to translate form labels, error messages and notifications.
Is this the correct way to do it or is there a different approach which does not require to inject a translator instance into every object having a text needing a translation?
(I know I can translate validation errors by injecting a default translator to AbstractValidator, but I prefer to have my own error messages)
use doctrine module for zf2 as orm and let Doctrine extension gedmo/translatable handle the data translation https://github.com/l3pp4rd/DoctrineExtensions
To get the Translatable feature working, follow this part of the documentation:
https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/zendframework2.md#note-you-may-need-to-provide-additional-settings-for-some-of-the-available-listeners
After that run the schema tool to update your database.
I'm trying to make some kind of "reusable" base controller in Symfony 2. Given the name of an entity, is it possible to read the schema of that entity? That is, a list of the entity's fields with the name and the type of each one.
This is in order to automatically generate the inputs in an admin panel and other similar operations.
SensioGeneratorBundle provides CRUD generation capabilities.
Its source code is available on GitHub.
Its documentation is well written; you should probably read it before making your decision.
This bundle can generate a CRUD controller based on a Doctrine entity. Once your application is set up, run the following command:
php app/console generate:doctrine:crud
It will generate forms, controllers and views.
Generated classes are easy to extend.
I'm working on a Symfony 1.4 project with Doctrine 1.x, and functionality that merits using a Doctrie_View (as an interface for native MySQL Views).
As I understand it, the View (as in DB View as opposed to the View in MVC) has to be created wth Doctrine so that Doctrine can maintain the association between the View and the original Model from which it's derived.
In an ideal world, I'd like to have the View created as part of the symfony doctrine:build --db task. The sensible way to do this would be to use the observer pattern and Symfony's Event Dispatcher, however, the list of Built In-Events doesn't seem to offer an event for when the database schema is built.
So, what's the best way to have a Doctrine View created when the schema is built?
Or perhaps, if that's not an option, check if a View doesn't exist and then create it as part of ProjectConfiguration::configureDoctrine()?
doctrine:build-sql
I think you should rather look at the doctrine:build-sql task which builds the sql instructions from the model definition.
If you look at the sfDoctrineBuildSqlTask class you'll notice it's rather simple. It really only calls the doctrine cli. If you want to hook into it you should check the Doctrine events rather than symfony.
migrations
What you could also do is creating your view in doctrine migrations. Whenever you need to change your view you'll create another migration (removing old and creating new view).