Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 update from entity

Is it possible to update an entity in a similar way as below:

$data       = new ATest();  // my entity $data->id   = 1;            // id 1 already exists, I just want to update this row $data->name = "ORM Tested"; // changed the name  $entityManager->persist($data); $entityManager->flush(); 

This will insert and change the id of the object instead of updating the existing row in the database.

like image 741
Dennis Avatar asked Apr 13 '11 07:04

Dennis


People also ask

What is doctrine entity?

Doctrine has a command-line interface that allows you to access the SchemaTool, a component that can generate a relational database schema based entirely on the defined entity classes and their metadata. For this tool to work, you need to create an executable console script as described in the tools chapter.

How doctrine works?

Doctrine uses the Identity Map pattern to track objects. Whenever you fetch an object from the database, Doctrine will keep a reference to this object inside its UnitOfWork. The array holding all the entity references is two-levels deep and has the keys root entity name and id.

What is doctrine database?

The Doctrine Project is the home to several PHP libraries primarily focused on database storage and object mapping. The core projects are the Object Relational Mapper (ORM) and the Database Abstraction Layer (DBAL) it is built upon.

What is doctrine repository?

A repository in a term used by many ORMs (Object Relational Mappers), doctrine is just one of these. It means the place where our data can be accessed from, a repository of data. This is to distinguish it from a database as a repository does not care how its data is stored.


2 Answers

You should call merge instead of persist:

$data = new MyEntity(); $data->setId(123); $data->setName('test');  $entityManager->merge($data); $entityManager->flush(); 
like image 177
Francesco Casula Avatar answered Sep 20 '22 07:09

Francesco Casula


I had to use

$entityManager->merge($data) 
like image 27
Dennis Avatar answered Sep 21 '22 07:09

Dennis