Is it possible to collect and map two fields from form to one property?
Details:
I have an array field in my entity:
#ORM\Column(name="custom", type="simple_array")
where admin can specify (multi choice) custom options for the product - for example:
$product->setCustom( array('customText', 'customNumber') );
So the user should have two fields in his order form - text and number. Then I want to save them:
$order->setCustomOptions(array(
'customText' => 'Foo',
'customNumber' => '100',
));
In order entity there is just array field type instead of simple_array.
User can't add new options so the collection field type isn't a good choice in my opinion.
I have also tried to use data transformer but I think it can be applied only to one field.
Tell me if it's not clear. I don't need a complete solution but a hint what to choose.
Finally, I created two additional fields in my Entity (like DonCallisto suggested) without mapping to Database and rendered them in the form. Then instead of using DataTransforem, I used a setter with #ORM\PrePersist and #ORM\PreUpdate to set custom values together.
Related
I'm new to Symfony and I would like your help in deciding the best way to design a form and store into database using Doctrine. Here is my task:
I have a form created with the FormBuilder containing various types of input fields. I have created entity which connects my database to form and it works both ways, both to fetch data and to put data into database.
However, I want to add a Select multiple field to a form which would be populated from a table from the database. And when I select the choices from that select element, I would like to save them into a database in either csv of id's or some simple serialized object or array.
So far, what I have managed to do is to add that Select field as EntityType and have placed ORM Annotation in entity for that element: Type("object"), which populates the Select element flawlessly. From the $_REQUEST variable I can see that it returns the id(s) of selected options. But, what I get when I submit the form is a very large serialized object in the field which is supposed to store selected element.
I suppose that it is a normal behavior (to store the entire object (an entire row of data)), but is there some cleaner, leaner way of storing only the id's that I would be able to later on use to populate the same select field, also using doctrine (since I don't have a need for other data in that field except for their IDs)?
Thank you in advance.
I am a newbie at Symfony2 and just hacking my way through some existing codebase. I need help in understanding how to achieve the following outcome.
I have a PriceList entity that stores:
DollarCostPrice, PercentDiscount1, PercentDiscount2, PercentCommission
I want the user to enter following 4 values through a form:
SalePrice, DollarDiscount1Price, DollarDiscount2Price, PercentCommission
where,
SalePrice = (DollarCostPrice + (PercentCommission * DollarCostPrice))
DollarDiscount1Price = ((DollarCostPrice + (PercentCommission * DollarCostPrice)) * (100 - PercentDiscount1)/100)
DollarDiscount2Price = ((DollarCostPrice + (PercentCommission * DollarCostPrice)) * (1 - PercentDiscount2)/100)
But once user has entered the above values, I will compute DollarCostPrice, PercentDiscount1, PercentDiscount2, PercentCommission that need to be persisted in the entity.
I want to use a collection of above 4 fields in a form so that users can enter this information for multiple items at once.
I am still new with using forms, collections and data-transformers in Symfony2. I would appreciate if someone could help me determine the best way to do it.
I would do it this way. Note I'm making assumptions here, you haven't provided any information on what entities relate to PriceList for example.
Create a PriceListType form type, you would have 1 mapped field (PercentCommission) and 3 non-mapped fields (SalePrice, DollarDiscount1Price, DollarDiscount2Price)
To add a non-mapped field:
->add('SalePrice', null, array('mapped' => false))
I'm assuming that PriceList has a relation to some sort of SKU object or similar. You need to consider how you manage this relation.
Start with a basic form for your PriceList Type. I'd use something similar to this technique for adding new PriceList items.
On Save I would initially just do the entity calculations in the controller to get things up and running quickly E.g.
$yourForm->handleRequest($request);
if ($yourForm->isValid()) {
// loop through elements in your collection - note for non mapped fields
// you need to access them like so: $form->get("DollarDiscount1Price")->getData();
// calculate the data you wish to save to each PriceList entity, set and persist it
$entityManager->persist($priceListEntity);
// finish & finally flush
$entityManager->flush();
}
Then later I might move that functionality to a form event listener.
I don't see a need for a data transformer here myself as it'll add complexity. If you're planning to re-use this functionality in a number of forms maybe, but otherwise I don't think I'd worry about it.
People will tell you X is the ideal solution, but IMO start simple and refine as you go.
I have an application where I use Codeigniter + Grocery CRUD.
I was making a "n_n" relation , but in table that store the relation there is a third field.
Bellow my DB structure:
Grocery CRUD allows me to select the socialmedia as displayed bellow:
The complete functionality is showed here: http://www.grocerycrud.com/documentation/options_functions/set_relation_n_n
How can I complete the profile field (commerce_socialmedia.profile) when a socialmedia item is selected in my form?
Someone can help me? Is there something already done for my necessity?
Thanks
i just create a Pull Request with this feature.
In order to use the extra fields functinality you need to pass one extra parameter to the set_relation_n_n function indicating that you want to edit the extra fields
$crud->set_relation_n_n('secialmedia', 'ecommerce_socialmedia', 'socialmedia', 'id', 'id_socialmedia', 'name', null, null, true);
You can also indicate that you don't want to edit one of the extra fields of the relation table by using the unset_edit_fields. You will however be required to prepend the name of the field you wish to exclude, with the string extra_field in order to avoid name conflicts
$crud->unset_edit_fields(array('extra_field_{relation_table_field_name}'));
Follow the link
https://github.com/scoumbourdis/grocery-crud/pull/275
In Symfony2, if I embed a collection of forms pointing at a many to one relationship in Doctrine and allow adding and deletion, if I delete a record from the beginning, add one at the end, and edit some in the middle how does the system know which records to update with which data?
There is nothing in the tutorial that passes the primary key of the embedded data around. Under certain circumstances, my records are getting needlessly deleted and added again rather than edited in place (even if there are no changes to the particular record). This breaks the fields on the records that are not included on the form, setting them to their default values from the DB model.
Is there a way to pass the primary key in the form and have it used to perform updates when the data comes back?
If you want to index the collection (by the entity id) for all querys, you can simply use the indexBy annotation in your entity class.
/**
* #ORM\OneToMany(targetEntity="EntityClass", mappedBy="EntityVariable", indexBy="id")
*/
private $collection;
Based on the Akkumulator's answer and comment and some experimentation, I did this:
Create new fields (using Javascript as described in the documentation) with __name__ replaced not by a number but by a string: new_ followed by an forever increasing number that has nothing to do with the list (e.g. new_1, new_2, new_3...)
I don't have to push the primary keys into the forms and I don't need indexBy either - that's good, because indexBy felt like it was too far removed from the form, ending in having the Action at a distance anti-pattern.
Why this works:
PHP arrays aren't like those in other languages. They're always dictionaries, so you can add string keys to them even if they only have numeric keys to start with.
Because the Symfony collection is mapped by field name, new fields will not match existing data and deleted fields will not be matched to existing data (and thus be removed from the set)
One way to pass primary id is to use INDEX BY.
For example, say I have an entity called Customer and a Customer has several Emails. In my Customer repository class, I can specify my collection to be indexed by Email's primary id.
$qb->select('c, e')
->leftJoin('c.emails', 'e', null, null, 'e.id')
->where('c.id = :id');
By doing so, the generated name of the input tag would be
customer[emails][e.id][fieldName]
Upon submitting the form, Symfony will bind the request values according to the input names.
I'm new to zend framework but have made my first steps with it successfully.
Until now I have created some Zend_Forms which are mapping single records of my model
to the form fields. I have handled the forms with form classes for each case.
This works all very well until now.
Now I have the situation that I have to asign features to a product. Features and products are parts of my application. Features are stored in my database in three tables. For each feature there is one record in the third table.
First is the feature group where the name of the feature group is saved. Every feature should be asigned to a feature group.
Second table is the features table. This table has an foreign key to the feature group and the name of the feature.
Third table is some kind of many-to-many relation which connects features to products. This table has an aditional field which contains an optional value (beside the two foreign keys) for this unique feature of the product.
For example: if the product has a weight of 4,78 kg the value "4,78" is stored in the third table and the label "weight of %s kg" is stored in the second table. The feature group could be something like "physical attributes" had is saved in the first table.
To cut a long story short:
My problem is how to handle the case that I have to create and edit multiple database records in one form. The plan is to have a form with many checkboxes for each for a feature whereby features are thematicaly grouped. Every checkbox should have an aditional text field to input optional values.
you could make a custom form class that extends Zend_Form and use that for you classes.
It could take in the construct instances of your models and construct the form inputs based on that models.
After form validation in your controller you can do
$values = $form->getValues();
and use that array to populate your models again
You can try creating subforms (Zend_Form_SubForm) inside your form class. This can separate fields for different tables. For edition, in your controller, when you pull all the data from the tree tables, you can populate subforms that correspond to the tables.
You can try to extend Zend_Form to create your own elements.
You will be able to write a class that connects to DB to get attributes (features & products).
Assuming you wrote My_Form_Element_Features & My_Form_Element_Products classes, you can do $features = new My_Form_Features(); and then use the base class methods like getValues(), populate(), etc.
You can take a look there to start :
http://framework.zend.com/manual/en/zend.form.elements.html
http://smartycode.com/extending/database-aware-select-elements/
--
To answer to your comment, you can use :
Zend_Form::setElementsBelongTo($array):
More information can be found at Zend_Form Advanced manual page.