user2794692
user2794692

Reputation: 391

How to save data from the view in Symfony2

How can I save datas in my DB from a view in Symfony2.

If I have a entity user and I use:

 $user->setInformation("Test");

That information doesn't stored in the DB.

Thanks in advance

Upvotes: 0

Views: 57

Answers (2)

ishenkoyv
ishenkoyv

Reputation: 673

View shouldn't contain entity persistence/save logic. You should use view only to represent data you get from controller.

$this->getDoctrine is alias to get default entity manager from DI container

So

  • You should check which doctrine entity manager you use
  • Ensure you can access dependency injection container. In controller you can use $this->get() or $this->getDoctrine() as container is alredy there
  • Set fields
  • If you create new entity you should persist it with $em->persist()
  • Flush changes to database with $em->flush();

Upvotes: 1

Ramesh
Ramesh

Reputation: 4283

You should persist and do flush to save the changes in the database. Assuming you are using Doctrine ORM, the below code must work.

$user->setInformation("Test");
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();

Upvotes: 1

Related Questions