S
S
Stepan Yudin2015-07-17 14:19:19
symfony
Stepan Yudin, 2015-07-17 14:19:19

How to implement such a relationship in Symfony (and display it on the entity form)?

Hello!
Question on Doctrine and Symphony.
It is necessary to make the following mechanism of links in the application:
1) There is a product group entity
2) There is a product parameter entity
3) There is a product entity
There are the following links:
- The product is linked to the Product group (Many-to-one, links are stored in the special field of the Product)
- The product parameter is linked to the Product group (Many-to-many, links are stored in a separate table)
. Thus, the Product parameters are not explicitly related to the Product entity. Parameters and Products are linked through the parent Product Group of a particular product.
What is required is this: when creating/editing a product, the form should display all the Product Parameters linked to the parent Product Group of this Product. And how to store parameter values ​​for each individual product?
How to implement this, gentlemen? (sorry for the goods goods goods goods...)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2015-07-17
@stepan_sib

Well, first of all, it is the task of the client. Ideally, you should have 2 forms, not just one.
Without client intervention, you can solve this problem with two submit buttons.
If js works, then either through 2 submits, or through one form, where a Parameter is added to the Product with the mapping turned off using the listener.
1.
The form is sent with an additional button, the same js when changing the Product group.

// ProductGodType
$form->get('nextStep')->isClicked() {
    // add Parameters
}

2.
Here, when changing the Group, js requests a form with the parameter //example.com/myForm?group=X
// PtoductType
$builder->add('parameters', 'choice', [
    'data' => $options['parameters'],
    'mapped' => false,
    //...
}

The point is that by means of Symfony (and this is a server) - the problem is solved by atomic forms. With the participation of the client, there are a lot of ways to assemble this form. (given 2 simple examples).
The third option is the easiest, but without rendering on the server.
js we request form pieces, assemble the form and send regular json
{
  "name" : "ProductName",
  "group" : {
    "parameters" : [
         // ...
    ],
}

And on the server, using the same jms, we get the product object with all the links.
Validate (
if ($this->isXmlHttpRequest()) $this->get('validator')->validate($object);
) and, if successful, save it to the database.
Thus, you will generally get rid of the assembly of the form on the server, without the "disappearance" of validation (Will be: 1 - when deserializing, 2 - when checking with a validator).
If you need to clarify something - write. It all depends on the solution method, of which there are plenty (up to changing the base architecture)

K
Konstantin Shirkin, 2015-07-17
@skw85

+ One more parameter entity for each individual product:
"product id"
field "parameter id (from 2 p.)"
field "parameter value " field

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question