Foreign entity in form into different kind of input - forms

I have two entities: product and category (Symfony 2.3).
I want to create a form in which an user can choose a product by first selecting the category. A user selects the category by clicking on image, then I want to set the image's value into a hidden input, but I don't see how can I change a foreign entity choice list to a hidden input (http://symfony.com/doc/current/reference/forms/types/entity.html).
How can I accomplish this? (how to change form input to hidden)
If I set cascade validation to true, will it for example check if a category really exist. (To prevent putting products with non-existing category from malicious users) ?

Part 1
To do this you need to use a data transformer to do two things:
transform an entity into an identifier that is either a string or integer so a form can render it as a hidden field.
transform the string or integer identifier into the entity when the form is submitted so that the parent entity can be saved with the correct relationship
The symfony docs I linked to above (here too) actually walk though an entire example of using a data transformer with a form.
As a shameless plug (because I believe it is helpful) I have written a little tutorial on using a data transformer for a hidden field with an entity id: http://lrotherfield.com/blog/symfony2-forms-entity-as-hidden-field/
Part 2
If you are using the data transformer then you don't need to worry about malicious users. The data transformer will fail because it will not be able to reverse transform the category from the fake id. In my tutorial the transformer will throw a Symfony\Component\Form\Exception\TransformationFailedException exception.
You can also write a validator (potentially using a call back) if you wanted that checks that the submitted category is real if you want an error to show in the form. Doctrine wont allow you to persist a fake category relationship as the foreign key constraint will fail.

Related

Microsoft Master Data Services 2016 Additonal Domain Atrribute Referencing

Is it possible to reference additional columns apart from the 'Code' and 'Name' columns when using a domain attribute in an entity?
E.g. A person entity has a code of '1' and a name of 'Smith' and a Gender of 'Male'
In a customer entity there is a domain value referencing the person entity which displays the following 1 {Smith}. The users would like an additional read only attribute which would copy the Gender value of 'Male' into the customer entity based on the domain value. Can this be done using out of the box MDS UI?
I know this is duplicate data and breaks normal form but for usability this would be useful. It would be the equivalent of referencing additional columns in an MS Access drop down list.
Many thanks in advance for any help
This is not possible with the standard UI. One option would be to develop a custom UI where you can handle these kind of requests.
If you want to stick with the standard product I can see a workaround but this is a bit of a "dirty" one.
You can misuse (abuse) the Name attribute of the Person entity by adding a business rule to the Person entity that generates the content of the Name attribute as a concatenation of multiple attributes. You of course need an additional attribute that serves as a place holder for the original Name. The concatenated field will then show in your customer entity.
One question that does come to mind is why a user would like/need to see the gender of a person in a customer list? As you have a separate Person entity I expect you to have multiple persons per customers. What would the gender of one person - even if it is the main contact - matter?

Document based core data app - add and remove additional attributes

I have a document based core data app with entity "Languages". This entity has two permanent attributes "key" and "comments".
Is it possible programmatically add and remove additional attributes during runtime ("language_1", "language_2", etc.) ?
My goal is to avoid creating table with let say 50 attributes when user needs only few (I don't know upfront how many attributes will be necessary).
Or maybe I should choose other solution ? :)
EDIT
Case explanation:
When user creates new document, table "Languages" has only 2 attributes "key" and "comments". During working with the document user can any time add or remove language(s) - I mean attributes (columns) not rows in the table.
My goal is to have dynamic entity like below.
Yes, it's possible. But it's probably not what you want. You'd have to recreate the amended Managed Object Model, for each document, at runtime whenever the document is opened.
After seeing your sketch, I suggest a slightly different model. By the way, best style is to use singular nouns for Entities ("Section", not "Sections), plural nouns for to-many Relationships ("sections", not "relSection"), and omit the entity name in its attributes ("comment", not "sectionComment").
Use one Entity for your permanent attributes. Call it "Word". Word has attributes "comments" and "key", and to-many relationships "translations" and "sections". On the other end of the "translations" relationship is a Translation entity, which has attributes "text" and also perhaps the name of the language (either as a string or as another relationship).
Something like this:
For your first example, you'd have one instance of Word, 3 instances of Translation (.text = Home, Zuhause, and Casa), and 3 instances of Language (.name = English, German, Spanish). When you add the second line, you'll get 1 more instance of Word, 3 more instances of Translation, but 0 more Languages. Add the new Translation instances to the existing Language's "translations" relationship instead.

Using FormBuilder to generate an edit form for a many-to-many join-entity with extra fields

tl;dr
I need an edit form for a uni-directional many-to-many relationship (doctrine), where the resulting join table has extra inherited properties. Is there a way to achieve this through the FormBuilder?
Entities
I have a uni-directional many-to-many relationship (doctrine), where the resulting join table has a relation to a third entity.
Form
The User edit-form should allow (un)crossing Deals easily through a checkbox, while also showing the relations' inherited fields from Discount. The discount's properties should be inherited from Deal by default and overridden on the UserDeal object if modified.
My approach
The way I have approached this, is by creating an auxiliary object UserDealState with two properties: a boolean active and a reference to the UserDeal. UserDealState has its own Type (checkbox and Discount fields).
I also added a transformer that converts the array of UserDeals to an array of UserDealStates, adding all the non-joined Deals (and setting active to false).
When I check the form inspector in the Symfony Profiler, I can see the transformation has taken place (the view data holds a collection of UserDealStates and is a much longer collection than the model data that holds a collection of UserDeals, which makes sense), but the form object only holds the amount of UserDealStates formViews that the original collection had of UserDeals had, not the much larger number it should.
Just to give a concrete example: say we have 10 Deals, and only one crossed UserDeals. On my form {{ form.userDeals.vars.value|length }} returns 10, {{ form.userDeals.children|length }} returns 1. Only one subform is rendered instead of 10.
Hera are my form-types: UserType, UserDealStateType, UserDealType, DiscountType.
Transformer: UserDealStateToUserDealTransformer
The question
How do you render a form with FormBuilder to edit the mapping between two entities in a many-to-many relationship and the relations' extra fields (as shown on the form mock-up above)?

Symfony2: Collection of dropdown select lists for a many-to-many relationship

The objective:
Having a many-to-many relation be displayed as a dynamic list of select inputs(single choice dropdown list)
User arrives on page with a single select field (multiple = false) populated with persisted entities and add/remove buttons. By clicking the add button, a new select field with the same options appears below the first, which adds a new entry in the M2M relation. By clicking remove the field disappears and the entry should be removed.
The model:
Two entities: User & Manager. A User has exactly one "special" Manager and unlimited normal Managers.
Managers manage unlimited users.To model this I have created two relationships for which the user is the "owner" (not sure how to translate this)
ManyToOne specialManager
ManyToMany normalManagers
I haven't created a many to many relationship with attribute "special" because the requirement is exactly one special manager and I wasn't sure if Symfony/Doctrine would cause problems down the line.
What I have:
I can display a multiple select field with the existing entities using Entity field type, as per the documentation. Functionally this is what I need, visually it is not.
I can also use the Collection field type to display a single text field, and add or remove more with JS, as per the documentation. Visually this is what I need, but The text fields (entity attribute) need to be replaced by choice field.
The question:
Before I continue digging, is there a simple way to achieve this list of select tags?
For anyone else who may eventually need a dynamic list of select fields:
I initially solved this issue by detaching the field(s) in event listeners, and handling the display/submission manually in the controller.
However I wasn't satisfied with this clunky solution and when I encountered the same need I used a second solution: creating an intermediary entity xxxChoice (in this case ManagerChoice) which is Mto1 inversed related to User and Mto1 related to Manager. Then by creating a ManagerChoiceType form with "Manager" entity field type I was able to easily display my collection of dropdown select lists.

How to use Entity, Mapper, Service and Hydrator in ZF2

I am making a ZF2 app. I am using entities, mappers and services (e.g. UserEntity, UserMapper, UserService) to manage the objects/models. Properties in the entities are CamalCased (e.g. FirstName, LastName) while in the database, fields are using underscore (first_name, last_name). I will plan to use a hydrator to map the properties and db-fields when retrieving or saving. The service object (UserService) will be used to communicate with the mapper to retrieve and save data models using the mapper. The hydrator will convert the result of mapper and convert them into proper entities.
The thing I am confused is that when the service (UserService) need to provide some cirteria - for example to find all users with a specific 'last name', will the service use the database field names (last_name) or entity properties name (LastName)?
If the db field name is used in the Service, so any change in the db structure will require me to update the service also, which completely fails the reason of using the whole approach.
If you take a look at the ClassMethods:hydrate method (https://github.com/zendframework/zf2/blob/master/library/Zend/Stdlib/Hydrator/ClassMethods.php) you will see that it just copies the properties from one object to another. You have the option of converting the property names to/from camelCase but that's it.
If you change a column name in your database then you will need to change corresponding property name in your object. And vice versa. Which I believe is the crux of your question?
If you want to make table column names be independent of your method names then you need something that lets you define an actual mapping table somewhere. Change a column or method name and you only need to update the configuration mapping table.
Not a ZF2 expert so I could be wrong but it doesn't look like any of the supplied hydrators support this.
I do know that Doctrine 2 supports it.