Symfony2 : datatransformer for child object - symfony

Let's say I have Category hasMany Product relation.
Product entity has name and price columns.
The problem:
I want to edit Category entity with its collection of Product entities using data transformers. For example:
Edit form is empty. I click on "add product", javascript would call ajax and new Product will be created. That newly created product has id that will be injected into the main form (as hidden), together with field for name and price.
So I need transformer that will understand the hidden ID and edit name and price on that object.
What I have is kinda big code and Symfony tries to create new object.
NOTE:
I am aware how to work with collections but my real relation is not as in this example and javascript/ajax must create new Product.

Related

OrangeHrm Add Field in PIM Personal Details

I am new to OrangeHRM 3.3.2 I need to add new field in the Personal Details. I need your help. Thanx
You can add fields to your OrangeHRM application by doing one of these.
1) Edit the database to contain your new field. Then edit the application form template using a new ohrm_widget (ohrm widget means an input field in the OrangeHRM system (ex. Test Field, Calendar, Dropdown, etc.)) and edit the controller function to pass your data to the backend. Then complete the DAO layer function to save your data at the database.
Ex:
If you are going to add a preferred name field for employees, then,
Add the preferred name column to the data table
Add the new preferred name input field to the form template
Add the new parameter in the controller layer to pass it to the backend
Complete the DAO layer function to save the preferred name parameter in the data table
2) In OrangeHRM 5.x you have the support from the application itself to create the custom fields for your entities. (I don't know about the 3.x versions.)

Embedded form persistence : get or create

I work with Symfony2 and doctrine. I currently have an entity called Person. This entity is related to some other entities as a One-To-Many relation (as a Many-To-One unidirectional relation). I want each Person entity to be unique (what I have done in my database by using the UniqueConstraint annotation).
To be clear, I will assume that I have two entities called Home and Car which both have a Many-to-One relation to the target entity Person.
Then I am using forms to create or edit my entities Car and Home. In these forms, I display a embedded form to create a Person entity or select one existing. I explain : the first field of my embedded form is the name of the person. As the user type the name of the person, a list of the existing persons is displayed (using JQuery Autocomplete UI) and if the user select one of them the other fields are autocompleted.
The issue is when the user submit the form with an existing person, I caught an Integrity Error (and I know why, because of my Unique Constraint).
One of the first workaround is to add the id field as an hidden input in the embedded form.
But then the user can edit the other fields and corrupt the current entity.
So no.
Another one could be to prevent the persist in the controller if the Person already exist, but as I am using this in many other entities. I will have to duplicate my code, and I do not want to, as the unique constraint is related to the Person entity and not to the Car or Home entity.
So no again.
The workaround that I am working about is to use a PrePersist Listener waiting for a Person entity, but I do not know how to cancel persist (and maybe it is not possible).
I have the following code :
public function prePersist(LifecycleEventArgs $args) {
$entity = $args->getEntity();
if($entity instanceof Personne) {
$em = $args->getEntityManager();
$persons = $em->getRepository('MyBundle:Person')->findBy(array(
// unique fields
));
if(count($persons) > 0) {
// ... ???
}
}
I have tried a $em->detach but it is useless as I am already persisting the entity.
What I want it is just a kind of a "Get Or Create". I explain, there are only two cases :
the Person entity (not persisted) has all the same fields that one existing in the database (excepted the id field), so the Person entity is the one in the database. I have to "replace" it by the one in the database ;
the Person entity (not persisted) is unique in the database, so we create a new one (persist).
Create your own getOrCreate() method and call it inside your listener.
See this post Symfony2 doctrine FindOneOrCreate
Another possibility would be the data transformers. http://symfony.com/doc/current/cookbook/form/data_transformers.html

nopCommerce 3.0 How to get user selected store and category on product detail page?

I want to show user selected store and category on product detail page like this
Store Name Category Name > Product Name.
How can I get user selected store and category from Product detail page. I use nopCommerce 3.0.
For the store name, you can use the store context object:
_storeContext.CurrentStore
But for the category, are you aware that a product can belong to multiple categories in nopCommerce? This is stored in the ProductCategories collection of the Product entity.
I have a problem with
_storeContext.CurrentStore
This everytime gives me wrong store information and the reason is described here
plugin-get-current-storeid-multi-store
So i have used the below code which i got from looking NivoSlider Plugin .
var storeScope = this.GetActiveStoreScopeConfiguration(_storeService,
_workContext);
_storeService is type of IStoreService
_workContext is type of IWorkContext
Use it via dependency injection from constructor.

How to deal with the choice "other" in EntityChoiceList in Symfony2 forms?

I've got a model as described below :
I've also got a form to create a new product with a field entity building a dropdown list containing all the Brands.
Now I want to add a value "Other" in this list in order to allow the user to specify the Brand manually in another text field.
The question is: is there a clean way to manage this case (eg. adding the value "Other" in the list, which is not an entity and get the form validation to work) with Symfony2 forms?
You can do it in two ways,
You can subscribe for FormEvents::BIND_CLIENT_DATA form event. In the event method you can create new Brand object from the text, save it and set the id to the form by calling $event->setData($data). See this cookbook entry.
OR
You can append a data transformer. In its reverseTransform method you can create+save the object and return its id. See this cookbook entry.

Texbox custom representation for Symfony2 entity form type?

My entity Person has a one to many relation with Tag entity. When creating a new person i'd like to show a textbox for assigning tag names separated by commas. User can then input an existent tag name or a new tag name (in this case, a new Tag entity is created and persisted).
Unfortunately entity field type has only checkbox and select representation. How can implement my own textbox representation?
You need to create a custom form type along with a custom data transformer.
This article is a pretty good guide on creating your own form type.
Also, check out Symfony's own source for the EntityType to get an idea of what's going on.
One way would be creating a custom data transformer.

Resources