Date: Fri, 29 Mar 2024 12:16:08 +0000 (UTC) Message-ID: <174446936.7943.1711714568762@ip-10-10-7-29.ec2.internal> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_7942_777993535.1711714568762" ------=_Part_7942_777993535.1711714568762 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
COmanage Registry can track each change made to a Model through the use = of Changelogs. When a record is modified, the old values are archived in or= der to provide an audit or change log of the modifications that were made.&= nbsp;Changelogs are implemented using a custom Cake Behavior (ChangelogBeha= vior).
Changelog Behavior operates using a modified copy on write=
design. When a record is changed, the old values are copied to a new recor=
d, and then the original record is updated with the new values. There are t=
wo reasons for this design: it avoids massive rekeying when a record is upd=
ated (otherwise related Models would all need to be re-keyed to point to th=
e new record), and because it's tricky within Cake to rewrite the record ID=
in the middle of an operation. (That is, for /foos/edit/23
, C=
ake really wants the item being edited to be #23, and not some new num=
ber.) It is simply easier to make the archived copy the new reco=
rd.
This behavior can be modified using the model variable $relin=
kToArchive
. If a Model defines a related hasOne or&nbs=
p;hasMany Model in this variable, then ChangelogBehavior will reli=
nk the related model to the archive copy, as part of the afterSave callback. This is useful for maintaining historical context, such as f=
or Petition attribute definitions. If the Model relationship is be=
longsTo, a similar rewrite happens, but in
beforeSave
and with the assumption that the parent model is what was changed. (The&n=
bsp;belongsTo relationship is used for when related Models are sav=
ed simultaneously as part of a saveAll or saveAssoci=
ated call.)
See also: Understanding Registry Deletion
Behavior Priority
ChangelogBehavior must execute bef= ore ContainableBehavior in order to properly filter results from contain'd = finds. The default priority for behaviors is 10, so setting the priority to= 5 will cause the ChangelogBehavior to execute first.
NormalizationBehavior should execute before ChangelogBeha= vior in order to clean up data before any save happens.
As with other Behaviors, the appropriate Model should define something l= ike
$actsAs= =3D ("Changelog" =3D> array('priority' =3D> 5));
In order to support Changelogs, a Changelog-enabled Model must define se= veral fields. As described bel= ow, these fields are used to track changed records:
revision
deleted
actor_identifier
model_id
For performance reasons, model_id should be indexed.
$relinkToArchive
should a=
lso be defined if needed, as described above.
class M= odel1 extends AppModel { public $hasMany =3D array('Model2'); public $relinkToArchive =3D array('Model2'); }
When a hard delete must be performed, ChangelogBehavior can be initializ=
ed with the setting expunge set to true. This setting is also supp=
orted by StandardController, via the controller level setting $u=
seHardDelete
.
$model-= >reloadBehavior('Changelog', array('expunge' =3D> true)); class ModelController extends StandardController { public $useHardDelete =3D true; }
About actor_identifier
actor_identifier stores the username (identifier) of whoever ma= de the change. This is a string rather than a foreign key into cm_co_people for several reasons:
schema.xml
before cm_co_people.Extended Attributes
Extended Attributes are handled differently. They are treated as "extens= ions" of CoPersonRole, and archived copies are manually maintained by CoPer= sonRolesController.
ChangelogBehavior intercepts find, save, and delet= e requests to transparently manage the Changelog records.
revision
is set to 0.actor_identifier
is set to the current username.model_id
is set to point to the parent record.revision
is incremented.If the Model defines related models in $relinkToArchive=
code>, those related models will be re-keyed so that the related records po=
int to the archived attribute instead of the current attribute. (By default=
, related models remain linked to the current attribute.)
deleted
is set to true (effecting a soft delete), unl=
ess ChangelogBehavior is initialized with the setting expunge=
set to true (in which case a hard delete is performed).A find with the changelog
parameter archived=
set to true will return all records, not just those described =
above.
$args['= conditions']['Model.field'] =3D $requestedField; $args['changelog']['archived'] =3D true; $Model->find('all', $args);
By passing the changelog
parameter revi=
sion
, a find can request a specific revision for a record:
$args['= conditions']['Model.id'] =3D $requestedId; $args['changelog']['revision'] =3D $requestedRevision; $Model->find('first', $args);
|