waner
waner

Reputation: 67

Symfony - store data on another entity

Basically I create a form that uses multiple entity, and the result retrieved from this form I wish the stock in another table separated into BDD. I was told to make an insertion (or update) request in a repository and call it from the controller after checking the submitted data. But in this case there will be no persist or flush in this case since you do not save an object corresponding to an entity there will be no persist or flush something like that, but I arrive Not really have to do it.

That's why I want to store in another table:

enter image description here

When I validate my form, my result retrieved by my form is stocked here (id: 6)

this is my code :

public function testAction(Request $request ){
    $poste = new Poste();
    $formPoste = $this->get('form.factory')->create(PosteType::class, $poste );


    $historique = new Historique_employer();


    if ($request->isMethod('POST')&& $formPoste->handleRequest($request)->isValid()) {

        $historique->setPoste($request->request['poste_id']->getData());
        $historique->setCity($request->request['City']->getData());
        $historique->setEmployer($request->request['Employer']->getData());
        $historique->setTemps($request->request['Temps']->getData());


        dump($poste);die();

        $em = $this->getDoctrine()->getManager();
        $em->persist($poste);
        $em->persist($historique);
        $em->flush();
    }



    return $this->render(':stat:teste.html.twig', array(
        'poste' => $formPoste->createView(),
        'historique' => $historique,
        ));

}

and I would like store my data in this entity :

class Historique_employer 
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * @var Poste
     * @ORM\OneToOne(targetEntity="Poste")
     */
    private $poste;


    /**
     * @var City
     * @ORM\OneToOne(targetEntity="City")
     */
    private $city;


    /**
     * @var Temps
     * @ORM\OneToOne(targetEntity="Temps")
     */
    private $temps;


    /**
     * @var Employer
     * @ORM\OneToOne(targetEntity="Employer")
     */
    private $employer;

but when i do all that I have this error message :

Cannot use object of type Symfony\Component\HttpFoundation\ParameterBag as array

Upvotes: 0

Views: 846

Answers (1)

Gntem
Gntem

Reputation: 7155

Symfony 2.8 ParameterBag

you are accessing the request parameters like arrays use mixed get(string $key, mixed $default = null, bool $deep = false)

$historique->setPoste($request->request->get('poste_id'));

change the rest and you are good go.

Upvotes: 1

Related Questions