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.
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.
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.
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.
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.
You should call merge instead of persist:
$data = new MyEntity(); $data->setId(123); $data->setName('test'); $entityManager->merge($data); $entityManager->flush();
I had to use
$entityManager->merge($data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With