I'm a little bit confused how I should organize the bundles in symfony2. In my app I'll need 3 pages:
1- Insert
2- List
3- Update
Which one could be the right and best way to organize my code?
src/cp/AddPageBundle
src/cp/EditPageBundle
src/cp/UpdatePageBundle
OR
In one bundle write 3 different controllers, each one in a different file?
OR
In one bundle, write 3 different actions in one controller file?
I'm really confused with this.
Thanks in advance!
In one bundle, 3 different actions in one controller. There is no need to split this functionality across bundles or controllers.
For example you can edit/list/update User with UserController (insertAction, ListAction, UpdateAction) to deal with the user and for example add another controller (CommentsController) for edit/list/update comments. The same situation can be used for your Page example (add/edit/update)
Optional way is to create folder inside controller folder so that we have even more organized code. For example for create Admin folder for controllers: Admin/ConsoleController, Admin/CategoryController, Admin/PluginController to deal with admin functionality.
Exactly I agree with TroodoN-Mike. Also you should create PageBundle with your Page entity and your fields (publish_date, title, content ect), and execute
app/console generate doctrine:crud PageBundle:Page
Symfony will generate a basic CRUD, but with your insert/list/update
just tried this - command is:
app/console doctrine:generate:crud --entity PageBundle:Page
This starts up a wizard on the command line which will prompt you for any other parameters it needs.
Unfortunately only works if your primary key field is called id and has a getID() method.
Related
I have a Bundle which is the core of my website (we will name it CoreBundle).
But my website will be share and need to be modulable.
For example, I have an entity CoreApplication :
id
attr1
attr2
But now, I need to add 2 independent Bundle (part1 / part2) to the core, which will be added to the core depends on what the company needs. Both parts will do the same thing: add fields to Application.
For example, the company could install only part1. Or only part2. Or part1 and part2.
That's why I don't know how to start.
I could create one Bundle for each part and extends the Core. But if I add part1Bundle and part2Bundle, this will not work?
I could create an Interface InterfaceApplication and use resolve_target_entities...
How would you do?
Thanks!
This has been done before by OROCRM. Although I would try to avoid going that route unless it is your last resort.
How it works:
You create your bundles
Your bundle has doctrine migrations that updates the database schema
The entities gets populated from the database schema
Documentation:
https://oroinc.com/doc/orocrm/2.0/cookbook/entities/adding-properties
https://oroinc.com/doc/orocrm/2.0/book/entities
EntityExtendBundle Source code:
https://github.com/oroinc/platform/tree/master/src/Oro/Bundle/EntityExtendBundle
Hope you are all fine, I am a beginner trying to learn Symfony 3.0, so I want to generate my controller in a folder called Bundles, but it is not created, here is the command I taped :
php bin/console generate:controller
And this is fine, when I tape the controller name, that was what I wrote :
?[32mController name?[39m: Bundles/FrontBundle
But their answer is :
?[37;41m The controller name must contain a : ("Bundles/FrontBundle"
given, expecting something like AcmeBlogBundle:Post) ?[39;49m
I don't know if I have to do something before this to noy have such error.
Any Help would be much appreciated.
This is because controller name should be a combination of BundleName:ControllerName and you have provided a relative path to your bundle. If that doesn't work make sure you have your autloading properly setup and Symfony knows about your bundle
According to his doc you should use the shortcut notation when you are declaring a controller, entity, etc.
also you need to read the output of the command.
Welcome to the Symfony controller generator
Every page, and even sections of a page, are rendered by a controller.
This command helps you generate them easily.
First, you need to give the controller name you want to generate. You
must use the shortcut notation like AcmeBlogBundle:Post
Controller name: BundleNameEndingBundle:ControllerName
I'm using Visual Studio 2013 and ASP.Net MVC 5. I've created a bunch of views for my models and then I've changed them. I want to run scaffolding on some models and create some views automatically and then change the automatically-generated views. Is there another way other than re-naming some files or creating another solution and copying stuff?
Yes, you can re-scaffold by scaffolding the same model again, using the same model class and controller names as before. Your existing controller and views will be replaced.
Details:
Right click on your project or controller folder,
Add ... New Scaffolded Item,
MVC 5 Controller with views using Entity Framework,
Add
Choose your model and data class,
And ensure your controller name is the same as the one to replace.
I use version control - GIT to do it quickly and safely. I use Git Extensions (http://code.google.com/p/gitextensions/) user interface for git.
Have your code commited before re-scaffolding. Then re-scaffold the views, and go to staging (the button Commit in Git Extensions). It shows all changes that re-scaffold made and colors the new and deleted code lines. From there you can stage only the selected new lines, that changed in controller. After staging selected lines, reset the unstaged other changes.
There you have it! Your already modified code with new scaffolded parts. Do any edits and testing necessary and commit.
This is a new answer to an old question. It's somewhat similar to the existing answers, but I think different enough and easy enough to be of value.
1) Save the existing project/solution to version control just as good practice.
2) When re-scaffolding, use a different controller name which will create a controller class and it's 5 attendant views, but it won't overwrite anything that exists, preserving all your existing work.
3) Extract the appropriate methods from the re-scaffolded controller. Bindings for create/edit will likely change when the model changes, so capture those. Then delete the re-scaffolded controller.
4) That leaves the views in place to copy and paste the appropriate UI code for any new or redefined model properties. Once all the code needed has been copied, simply delete the re-scaffolded views.
It was a great question because we often have to change a model, and it's nice to have all the basic UI stuff automatically created for us.
I've started a Symfony2 project from scratch where I then installed FOSUserBundle.
Then, I have written (actually, generated with ORM Designer) some entities that need to have relations between them, and with the User entity.
I have Items belonging to Users, Collections belonging to Users that group Items, and so on.
Since I used FOSUserBundle I only have a basic User class (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md , step 3a) defined using annotations, no config/doctrine folder and no User.yml file in it.
I then created the MyBundle/Resources/config/doctrine folder and added the yml files mentioned above.
When I try to generate the entities with the command-line tool everything works fine: it will create the Entities from my yml files.
However, at this point, trying to load up in browsers the url where the login previously worked (when I only had the FOSUserBundle installed) will throw this error:
MappingException: No mapping file found named
'/var/www/concert/src/X/MyBundle/Resources/config/doctrine/User.orm.yml'
for class 'X\MyBundle\Entity\User'.
Following actions, such as generating the CRUD logic, will not work as long as I have an *.orm.yml file in the config/doctrine folder. If I remove those, CRUD generation will work, but generation of actual mysql tables won't.
Juggling with these gets me to a point where I can also get the tables, but then the actual app doesn't work if I try to use any of the url's where the newly generated CRUD is involved because since the entities are based on yml (which I remove to get things "working") it won't have any mapping knowledge.
Is this inherently wrong? To have yml-based entities in relationship with an User entity based on the FOSUserBundle and still be able to get the nice command-line generation tools?
The problem you describe stems from mixing configuration formats (yaml and I assume annotations). You can easily fix this by ditching the annotations in your models and replacing them with yaml-files like you would do in your own models.
Unfortunately the FOSUserBundle-docs only show you how to use annotations, so here is a quick transformation into yaml format when your X\MyBundle\Entity\User extends FOSUSerBundle's UserEntity:
X\MyBundle\Entity\User:
type: entity
table: fos_user
id:
id:
type: integer
strategy: { generator: "AUTO" }
The remaining stuff is taken care of by FOSUserBundle, as the BaseModel is a mapped-superclass and already describes the stuff in the User.orm.xml, but you could just as well replace the existing values or add additional values just like you would do with your own models.
If you don't use annotations throughout your app, you might also want to disable them in your app/config/config.yml to prevent side effects.
Going through the Symblog tutorial of Symfony2, While creating forms I came to a point where in I create Contact Entity (Entity/Enquiry.php) where I define some fields and some methods to access these fields. Then I create another folder Form/EnquiryType.php to build the form and then a contact.html.twig to display. I am unable to understand why we created 2 namespaces for Entity/Enquiry.php and Form/EnquiryType.php. when they have to deal with each other. Why dont we wrote both the classes within one folder or one file. And one more question. Do they belong to Controller or View part of MVC.
Form types are here to configure how data coming from objects (like Entities) are mapped to a form (and vice/versa).
Entities should'nt be named "entities", they should be just your buisness objects, that can be persisted through a layer called doctrine2.
To answer you on separation of concerns,
Entities are about M,
while form Types are about user inputs (so the VC).
View because it render a human interface to let user enter input,
Controller because that's where you handle the form lifecycle.
The reason is logical separation. Why don't we define all parts of MVC in one folder/namespace? Because it will be a mess. That's why logical separation is needed.
And not all entities have to have related form types — using entities without forms is normal.