Prettify your source code using model-entity-manager layer architecture.
I wrote this bundle to simplify my life when creating new Entity in Symfony2 project. I was using architecture of "3 layers" which split your ORM logic into few files: * Model - Contains only fields data and its getters and setters (and add, or remove if needed :D ) * Entity - Main file that is used to create Entity object, this file store internal Entity logic. eg. when you want to modify your data in some way. * Manager - And the Manager file which should be treated as "Repository" of given Entity and you should put here every QueryBuilder that concerns it., (*1)
For now this bundle supports mapping only in XML format. I think it's good practice to separate all these layers to improve readability of your code., (*2)
What is more important, every Manager gets it own Service so you are able to call it in easy way. More about it in Usage section., (*3)
Easy as pie :) Add to your composer.json file in require section:, (*4)
"codeage/entitygenerator": "1.*"
And update your vendors!, (*5)
Now the only thing left to do is enable this bundle in your AppKernel file, to do it, add:, (*6)
new CodeAge\EntityGeneratorBundle\EntityGeneratorBundle(),
As of version 1.1.0 traits were added to AbstractEntity in order to use methods (that I will add someday :) ) even when you cannot extend AbstractEntity for some reason, eg. if you are using FOSUserBundle and you need to extend their User Entity., (*7)
To create new entity use command below:, (*8)
$ php app/console ca:entity:generate #for Symfony3 $ php bin/console ca:entity:generate
Then you will be asked about name of your new entity, but you need to proceed it with bundle name in which it will be created (Command should autocomplete name of bundle :) ). eg., (*9)
UserBundle:User
Where UserBunlde is bundle name and User is your new entity name., (*10)
From now on, you will be asked to provide field name, its type (default is string) and its parameters. String for example can be configured with length and nullable value., (*11)
There is also "entity" type which is used to create relation field. (Note: For now only one-direction relations are available), (*12)
When you add all needed fields you will be asked to confirm generation of your entity, and several files will be created in your bundle directory (as described in about section :) ), (*13)
In your controller you can now call your manager in this way:, (*14)
function indexAction($id){ $userManager = $this->get(UserManager::SERVICE); /* @var $userManager UserManager */ $user = $userManager->find($id); /* @var $user User */ }
And that's it! As I said before, manager is used just like repository, so you can create some usefull queries in there to use insetad of "find" like in example here, but you can also use all respoitory-like methods as well eg. * findBy * findAll * findOneBy, (*15)
And what about insertions, updates and deletes?, (*16)
$userManager = $this->get(UserManager::SERVICE); /* @var $userManager UserManager */ $user = new User(); $user->setName('SomeNameOFUser'); $userManager->persist($user) ->flush();
$userManager = $this->get(UserManager::SERVICE); /* @var $userManager UserManager */ $user = $userManager->find($id); /* @var $user User */ //Assume that there is a user with this id :) $user->setName('NewNameForUser'); $userManager->update($user); //Done! But you can pass false as second parameter to disable force-flush :)
$userManager = $this->get(UserManager::SERVICE); /* @var $userManager UserManager */ $user = $userManager->find($id); /* @var $user User */ $userManager->remove($user); //Done! But you still can disable force-flush