Symfony form with prepopulated data - php

I'm having troubles finding the best approach to implement a Symfony form for the next scenario:
I've a group of entities to store a catalog of checklists (Checklists->Groups->Tasks->CorrectiveActions), those checklists can be implemented by the entity Stations.
In this implementation the station will give a score to each task, if the score is below 5 a list of corrective actions will appear for that task, each corrective action will have a checkbox, so the station can choose to implement it...
I've coded in raw html how the form would look like:
My problem is I don't know how to migrate this to Symfony type forms.
I don't know how to load a list of unmapped fields (groups, tasks and corrective actions) and then create a relationship with mapped fields (each task selected score and each task selecteds corrective actions)
Obviously I'm not asking for code I can copy and paste, I need ideas of how to implement this or some documentation that can help me with this.
I'm working with Symfony 3.1.0 and the next entities:
Checklist->ChecklistGroup->ChecklistGroupTask->ChecklistGroupTaskCorrective
v v v
StationChecklist -> StationChecklistTask -> StationCheckListTaskCorrectives
Didn't create StationChecklistGroup as I'm not going to add any kind of field to groups, or maybe should I add it anyway?
EDIT
In case it helps here is my db structure, maybe I could implement it better (some fields are missing). It's spanish, but I think you can understand it, punto_venta is my station entity:

Your question is pretty vague on some points so I'll try to help you as much as I can:
To handle collections of form elements (like your Groups of your Tasks), you should use CollectionType.
Having unmapped forms transforming data into real entities is an underwhelming use of the Form component, so try to have FormTypes with data_classes.
IMO you should have one FormType for each of your entities which maps it. So when a User creates a Checklist, render a ChecklistFormType, which is going to have a groups field (CollectionType of GroupFormTypes). Then create GroupFormType with a tasks field (CollectionType of CorrectiveActionsFormType) etc.
After that, the challenge will be to make your code reusable for Stations but I question a bit your model here: are StationChecklists really different than Checklists? And StationChecklistTasks than ChecklistGroupTasks? Maybe you could use interfaces or superclasses to map better these similar entities, and then have FormType mapping these interfaces/superclasses, instead of the actual child entities.

Related

Where should i put a new sql query function Symfony 3

I just started to work in symfony 3.4 and i'm pretty lost.
I want to make a CRUD for Products.The products table have multiple columns(name,description,CATEGORY_ID).
In create page, for this category_id, i want to make a select with the names of the categories that are stored in another table(id and category_name).
For this i need to select all the categories, but i don't really know how to do that.
I worked in codeigniter before and there i would make a function getAllCategories in category_model and i would call it in Controller.But here...i don't know how to do this.
Can you explain to me please how this should work?Where should i make the sql functions?
Your seem lost on things that are very basic for Symfony.
You probably have to start reading a lot and from the start.
You have two parts that you have to cover.
One is doctrine and how you can declare entities and manage them with the entity manager, and the second is Forms and how you can use them to render your form (including the foreign entity relation), to handle and validate your submitted data, and to pass the validated data to the database through the entity manager.
Your example is pretty simple, so it should be very easy to follow a tutorial while modifying it to fit your code.

CRUD : Adding Multiple Child Records (1-n relationships) on related CRUD form?

Running: Laravel 5.3 with Laravel Backpack CRUD 3.1
I am running into situations where I have a Model that I would like to add multiple related (child) records too, using just one CRUD form. Some examples would include adding multiple files... but let's start small. I have found the following posts that have similar topics, but not clear answer on the best way to do this.
Is the best way to use the table Field Type? https://laravel-backpack.readme.io/docs/crud-fields#section-table But, I guess the drawback is not having validation on the child records?
A similar tutorial to this one would be cool: https://backpackforlaravel.com/articles/tutorials/nested-resources-in-backpack-crud
So, an example would be where I have a Journey model and would like to add multiple Chapters to the Journey directly on the same Journey CRUD form.
Let me know if this question makes sense... and any suggestions/advice you can share.
Backpack doesn't support adding more entities in one form - every such form is very different.
My recommendation would be to edit the EntityCrudController::store() and EntityCrudController::update() methods, to check for the values of the "table" field and add/update/remove connected entries.

Can we set multiple hydrator for one zend\form?

Why? Because, I want to use them for attaching more than one Model/Entity for one Form.
What I think? In my opinion I have to get all data in one Model/Entity and then populate in multiple Tables (using Mapper or Gateway) but logically a Model is for one Table.
What is my proposed solution? So for adding one Form data in multiple tables, I have to use two Models. But in ZF2, a form can have one Hydrator using $form->setHydrator.
What I want to know? Any other possibility for this porblem.
Thanks.
Have you considered using formCollections?
Basically you would make a fieldset for each entity, with its own fields and hydrator. Then make one form that includes these fieldsets.
Much like the tutorial here: http://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html
( I'm on my mobile phone right now so examples are hard to give, if you need me to elaborate, let me know )

Filter entity collection for form in Symfony2

I'm looking for best practices to fill a dropdown with certain entities. There is a rolesystem in which a table holds the permission for the entities. So imagine objects projects, users and userProjects. Last one contains userId, projectId and a few permission. In a form the user can choose those projects for which the user is granted.
Sure there are many ways to achieve this. For example a certain central service could return a prepared querybuilder that can be passed to entity form type. But in my opinion the more atractive solution would be a service that returns an ArrayCollection of granted entities. But how to bind them on a choice control? I guess that would be a good job for a modeltransformer but .. what would you recommend?
Many thanks in advance!
If you really want it - create EntityChoiceList and pass it to form as choice_list to choice field.

Entity relations without second entity (value from array)

first I appologize if the topic may confuse you. I try to explain my question. In an application there are a lot of information which are too small to build an own entity for that. Some examples gender or status.
Is there a recommended way to do that or is it still an entity with two to five value in a table?
What I need is the standard behaviour in forms (selectbox) and show the value by an id.
I would certainly create these as entities!
You may feel it to be overkill, especially when you are just populating select boxes. However, it will be required to create the correct entity relationships such as $user->getGender()->getName() etc I doubt that the Gender options will change but it will be a reusable class for all your other projects.
Remember that Doctrine and other popular ORMs will proxy access to the object so it will be called in a lazy manner.
You could also use a "view helper" of some description that directly queries the database for the values you want and displays the select options accordingly, while you are still using your new entities elsewhere.

Categories