Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 73 lines (56 sloc) 2.408 kb
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
1 Implementing the Notify ChangeTracking Policy
2 =============================================
1e719313 »
2010-04-06 -
3
46983465 »
2010-12-03 Finialized ReST doc changes, merged changes from latest Markdown docs.
4 .. sectionauthor:: Roman Borschel (roman@code-factory.org)
5
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
6 The NOTIFY change-tracking policy is the most effective
7 change-tracking policy provided by Doctrine but it requires some
8 boilerplate code. This recipe will show you how this boilerplate
9 code should look like. We will implement it on a
10 `Layer Supertype <http://martinfowler.com/eaaCatalog/layerSupertype.html>`_
11 for all our domain objects.
1e719313 »
2010-04-06 -
12
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
13 Implementing NotifyPropertyChanged
14 ----------------------------------
1e719313 »
2010-04-06 -
15
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
16 The NOTIFY policy is based on the assumption that the entities
17 notify interested listeners of changes to their properties. For
18 that purpose, a class that wants to use this policy needs to
19 implement the ``NotifyPropertyChanged`` interface from the
20 ``Doctrine\Common`` namespace.
1e719313 »
2010-04-06 -
21
46983465 »
2010-12-03 Finialized ReST doc changes, merged changes from latest Markdown docs.
22 .. code-block:: php
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
23
24 <?php
c2967b35 »
2013-04-21 Update coding standards
25 use Doctrine\Common\NotifyPropertyChanged;
26 use Doctrine\Common\PropertyChangedListener;
1e719313 »
2010-04-06 -
27
28 abstract class DomainObject implements NotifyPropertyChanged
29 {
c2967b35 »
2013-04-21 Update coding standards
30 private $listeners = array();
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
31
1e719313 »
2010-04-06 -
32 public function addPropertyChangedListener(PropertyChangedListener $listener) {
c2967b35 »
2013-04-21 Update coding standards
33 $this->listeners[] = $listener;
1e719313 »
2010-04-06 -
34 }
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
35
1e719313 »
2010-04-06 -
36 /** Notifies listeners of a change. */
c2967b35 »
2013-04-21 Update coding standards
37 protected function onPropertyChanged($propName, $oldValue, $newValue) {
38 if ($this->listeners) {
39 foreach ($this->listeners as $listener) {
1e719313 »
2010-04-06 -
40 $listener->propertyChanged($this, $propName, $oldValue, $newValue);
41 }
42 }
43 }
44 }
45
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
46 Then, in each property setter of concrete, derived domain classes,
a0a133b0 »
2013-04-21 Removed unneeded escape character
47 you need to invoke onPropertyChanged as follows to notify
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
48 listeners:
1e719313 »
2010-04-06 -
49
46983465 »
2010-12-03 Finialized ReST doc changes, merged changes from latest Markdown docs.
50 .. code-block:: php
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
51
52 <?php
1e719313 »
2010-04-06 -
53 // Mapping not shown, either in annotations, xml or yaml as usual
54 class MyEntity extends DomainObject
55 {
56 private $data;
57 // ... other fields as usual
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
58
1e719313 »
2010-04-06 -
59 public function setData($data) {
60 if ($data != $this->data) { // check: is it actually modified?
c2967b35 »
2013-04-21 Update coding standards
61 $this->onPropertyChanged('data', $this->data, $data);
1e719313 »
2010-04-06 -
62 $this->data = $data;
63 }
64 }
65 }
66
1bfeaf3e »
2010-11-01 Initial conversion from Markdown to ReST - Finalized Cookbook
67 The check whether the new value is different from the old one is
68 not mandatory but recommended. That way you can avoid unnecessary
69 updates and also have full control over when you consider a
70 property changed.
71
1e719313 »
2010-04-06 -
72
Something went wrong with that request. Please try again.