Maxime Meunier
Maxime Meunier

Reputation: 175

Add a child to a @OneToMany bidirectionnal with FOSRestBundle

I'm building a REST API with FOSRestBundle on my Symfony2 application. What I want to do is to allow my POST and PUT actions, let say on a Product entity who got a @OneToMany relationship with another entity called Risk, to add/delete the children I past in JSON.

Let me give to you an example of JSON:

{
    "cproduct": "ASSOC000000000999",
    "risks": [
        {
            //first risk fields
        },
        {
            //second risk fields
        }
    ]
}

This is a simple JSON (the real one got more fields but these aren't needed here). So here I've my Product ID (ASSOC000000000999) and I want to update this product by adding to him 2 new risks. I know that normally I would have to create a Risk with the product ID separately, but for the needs of my application, I need to make only one request to the database. I want my users to be able to create a product, then add one or more risks and only then persist it into the database.

In a second time I would like them to be able to delete a child (risk) if he doesn't appear in the JSON sent with PUT action.

Here an example, let say that product “ASSOC000000000999” got a risk “RISK1”. If I send this JSON:

{
    "cproduct": "ASSOC000000000999",
    "risks": [
        {
            “id”: “RISK2”,
            //other fields
        },
        {   “id”: “RISK3”,
            //other fields
        }
    ]
}

On persist I want RISK1 to be deleted.

How can I do that? I found nothing on the web about that, please help me. :-)

PS: Sorry for my English, this isn't my birth langage.

EDIT: I target what my problem really is.

When I send that JSON file with HTTP PUT verb:

{
   "cfam": "AUTE",
   "lpronom": "My Contract",
   "riss": [{
   "cris": "AS",
   "lris": "Organization Law of 1901",
   "lrisfic": "RCAD_FICHERCA9"
   }]
}

Doctrine does a SELECT on RIS (my Risk table is called RIS, so the collection is $riss in my PROduct entity) where CRIS = "AS", and that's my problem. Here I want doctrine to create a RIS if the composite PK {cpro, nprover, cris} doesn't exist, and an update if it exist.

How can I do that?

(Without using Symfony form if possible).

Here my API call :

http://localhost/web/web/app_dev.php/fos/api/pros/ASSOC000000000009_1

My putProAction():

public function putProAction($id, Request $request)
{
    $detachedEntity = $this->reqDeserialize($request, 'Namespace\Bundle\ProductBundle\Entity\Pro');

    // Here I need to explode my serialized PUT parameter ID
    list($cpro, $nprover) = explode('_', $id);
    $detachedEntity->setCPRO($cpro);
    $detachedEntity->setNPROVER($nprover);

    $em = $this->getDoctrine()->getManager();

    // I use merge to attache the entity to perform the persist
    $entity = $em->merge($detachedEntity);
    $em->persist($entity);
    $em->flush();
    return $entity;
}

Upvotes: 1

Views: 542

Answers (1)

Maltronic
Maltronic

Reputation: 1802

You should use Symfony's Form Collections.

The example listed on that page is very similar to your requirements.

Upvotes: 1

Related Questions