-
Notifications
You must be signed in to change notification settings - Fork 3
Using EntityManager on the server
Although Flextrine allows database changes to be executed from the client-side, there are many situations where for reasons of application architecture or security you want to encapsulate these database operations on the PHP side instead. Flextrine provides support for this via Doctrine 2 and the callRemoteFlushMethod
.
It is very simple to use the Doctrine 2 EntityManager on the server, as long as you follow these rules:
- If you pass entities as arguments to callRemoteFlushMethod they will arrive as DETACHED entities. In almost all cases it is better to pass the entity identifier and then use
$this->em->find($id)
to retrieve the entity. - Do not use
$this->em->flush()
; instead use$this->flush()
, and return the result. - At present you may only ever use a single
$this->em->flush()
percallRemoteFlushMethod
Here is an example of using callRemoteFlushMethod
to create some entities on the server. Note that this example assumes the existence of some setter methods in the Doctor
and Patient
entities:
em.callRemoteFlushMethod("addPatientToDoctor", 1);
<?php
public function addPatientToDoctor($doctorId) {
$d1 = $this->em->getRepository('Doctor')->find($doctorId);
$p1 = new Patient();
$p1->setName("Patient 1");
$p2 = new Patient();
$p2->setName("Patient 2");
$p1->setDoctor($d1); $d1->addPatient($p1);
$p2->setDoctor($d1); $d1->addPatient($p2);
$this->em->persist($p1);
$this->em->persist($p2);
return $this->flush();
}
The result of the callRemoteFlushMethod
will be as in you had performed the same operations in AS3; the local Patient EntityRepository will contain the 2 new Patient
entities, and the new associations between the Doctor
s and Patient
s will be created and maintained. Furthermore the new Patient entities will be MANAGED and automatically watched for changes by Flextrine.
<?php
function persistEntity() {
$d1 = new Doctor();
$this->em->persist($d1);
return $this->flush();
}
Persisting new entities in a callRemoteFlushMethod
has the following effects on the client:
- The persisted entities are added to the relevant repositories
- The persisted entities become MANAGED
<?php
function removeEntity() {
$d1 = $this->em->getRepository('Doctor')->find(1);
$this->em->remove($d1);
return $this->flush();
}
Removing an entity in a callRemoteFlushMethod
has the following effects on the client:
- If the removed entity is in a repository it is removed
- If the removed entity is not in a repository there is no effect
<?php
function updateEntity() {
$d1 = $this->em->getRepository('Doctor')->find(1);
$d1->setName("A different name");
return $this->flush();
}
Updating an entity in a callRemoteFlushMethod
has the following effects on the client:
- If the updated entity is in a repository it is updated within Flex
- If the updated entity is not in a repository it is added to the appropriate repository
callRemoteFlushMethod
will be available from Flextrine 0.9.1 onwards