Can we set multiple hydrator for one zend\form? - php

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 )

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.

Symfony form with prepopulated data

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.

Where To Put Arrays For Common Data Storage In Symfony

I'm brand new to Symfony but am loving getting familiar with it (and many of the concepts behind it). MVC is pretty new to me in terms of the way I'm encountering it in Symfony.
My question is that if I have a simple array of commonly used data that I don't think necessarily belongs in a database table where should I store this. Is it an Entity? Should I store it in the Should I put it in the controller? Somewhere else?
I'm talking specifically about something like a US States array that I might use to power a dropdown. Right now I'm having to build an entity and store these in the database but would like to know if there is a better / preferred way to do this.
In my procedural days I would keep a file called "includes/arrays.php" and pull that when I needed one of these.
Thanks
If you want to use this data with other Entities, for example State would be connected to Adress object, I would stick with Entities, because it makes relations easier to implement and work with (I assume you using some kind of ORM e.g. Doctrine).
If you don't want to use this data with other entities, maybe you would like to hardcode them into all the templates somehow. http://symfony.com/doc/current/cookbook/templating/global_variables.html (I assumed you are using Twig).
A similar question was answered here:
Where to define static array related to an entity in symfony2 ?
It depends. I would opt by having that kind of data in the database. Suppose you in the future would have a back-office that update data.
Or you could use config files. For example, in yml format, arrays is easy to define.
Just like #foxtrot said, any data that is changeable should be stored in the database, just so you do not have to edit any code when a change occurs.
Firstly, I would create the Entity for the common data, and then I would use Fixtures to generate the entries in the database when you deploy your code.
This way, you allow later editing through either forms or phpMyAdmin, but you also get to write the default values into a PHP class so you don't have to manually enter all of them into the database.
See Symfony - DoctrineFixturesBundle

How to handle complex form with multiple models and tabular input in yii

I've read a lot of documentation about how to handle models and forms in yii and I've found a solution that works for the case that I explain following, but the problem is that the code is complex to write and to maintain, so I'm looking for suggestions.
The case is the following: I need to save together two different models and a third model that is a tabular input (more instances of the same model).
For instance, I may want to save a Blog post (first model) with the author information (second model) and a list of references (third model, the tabular one).
I'd like to validate all of them via ajax in one step and save them transactionally.
I've read all of these link.
http://www.yiiframework.com/wiki/559/tabular-input-validating-and-saving-related-models/
multi model forms in yii
http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/
http://www.yiiframework.com/wiki/218/how-to-use-single-form-to-collect-data-for-two-or-more-models-cactiveform-and-ajax-validation-edition/
http://www.yiiframework.com/doc/guide/1.1/en/form.table
http://www.yiiframework.com/wiki/362/how-to-use-multiple-instances-of-the-same-model-in-the-same-form/
http://www.yiiframework.com/forum/index.php/topic/14082-transaction-on-multiple-ar/
http://www.yiiframework.com/wiki/559/tabular-input-validating-and-saving-related-models/
https://github.com/yiiext/with-related-behavior
The last link is interesting, but if someone has one best practice to share, I'll be thankful to him.
I think you are looking for something like this: http://www.yiiframework.com/extension/eadvancedarbehavior/
This is an extension that does exactly that you asking.
Quoted from the link:
HAS_ONE and HAS_MANY
Suppose a User HAS_ONE Address and HAS_MANY Emails, and Address/Email BELONGS_TO User (both have the foreignkey column user_id).
So you can do now:
$blog = new Blog();
$blog->author = $author; //the author model object
$blog->references = array( $references1, $references2, ... ) ; //reference model objects
$blog->save();
I think you understand how to setup the forms? Those links you send are pretty clear about it.

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