Symfony2 - Updating an entity’s field when executing action in another entity’s Controller

Asked

Viewed 96 times

1

I need to update the field budget of my entity Client when inserting new data into the table Budget. Both have relationship oneToMany and manyToOne, respectively. I received suggestions to use the concept of OO to do this operation, so something more or less suggested this way for me:

if ($form->isValid()) {
    $manager->persist($form->getData());
    $manager->flush();

    $Client = $manager->find('PanelBundle:Client', $form['client_id']->getData()->getId());
    $Client->setBudget($manager->getRepository('PanelBundle:Budget')->getLastId());

    $this->addFlash('success', 'Novo orçamento adicionado');

    return $this->redirect($this->generateUrl('panel_budgets'));
}

When seeing the exit of $Client, when assigned to the variable the method find, the field is displayed name of id respective, which is the id of Client selected by a select and passed to variable when submitting the form.

Even if no error occurs, this field is not updated when registering. I tried otherwise writing a specific function to update, looking for examples, but got invalid semantic errors.

How could I update the field budget when executing the method addAction of the entity controller Budget?

1 answer

1

It’s all right in your code. You just forgot to persist the $Client and give the flush, the same way you do with the budget.

$Client->setBudget($manager->getRepository('PanelBundle:Budget')->getLastId());

$manager->persist($Client);
$manager->flush();
  • Hello! You’re making a mistake Warning: spl_object_hash() expects parameter 1 to be object, string given . I made some attempts to change but without success. The error is clear, I am passing a string instead of an object. I tried to instantiate the Client but nothing has changed. Do you have any other suggestions? Thank you!

  • You need to pass the budget object to the client, not its id. Do so: $Client->setBudget($manager->getRepository('PanelBundle:Budget')->getLast()) and create a method getLast in which you retrieve the last inserted budget.

  • If I did it right, it is returning an error that says the object cannot be converted to string, so I created a __toString() returning the address but still nothing is modified in Client. Check it out, I have another question on the global site http://stackoverflow.com/questions/32441630/trying-to-update-one-table-inserting-into-another-one-with-symfony2-and-do which I did differently. Would you be so kind as to look at and answer me here what you know about this method? Thank you, I’m getting slightly desperate already.

  • This error happens because you are probably trying to use the object as an argument in a form, but the form renderer does not know which attribute to use, and also that the method __toString() is not implemented. You have seen in the database if the object was entered and the budget for the client has been updated?

  • Using the object as argument in a form? It was not understood. As for the method __toString(), with or without it brings me problems, depending on how I leave the Controller. Either it gives an error saying that an array was expected, but a String was returned, or that an object came but an array was expected. It’s crazy and I don’t know what else to do. I’ll ask again providing more information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.