Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<com:TContent ID="body">
2
<h1>Test, test, again ...</h1>
3
<p>Of course, tweaking the Person List display is not going to be the end of it.
4
Clients always want more, and now ours wants to edit, add, or delete records.
5
Let's write some tests for these new tasks, as shown in the following.</p>
6
 
7
<com:TTextHighlighter Language="php" CssClass="source">
8
function testPersonUpdate()
9
{
10
	$expect = "wei";
11
	$edited = "Nah";
12
 
13
	//get it;
14
	$person = TMapper::instance()->queryForObject("Select", 1);
15
 
16
	//test it
17
	$this->assertNotNull($person);
18
	$this->assertEqual($expect, $person->FirstName);
19
 
20
	//change it
21
	$person->FirstName = $edited;
22
	TMapper::instance()->update("Update", $person);
23
 
24
	//get it again
25
	$person = TMapper::instance()->queryForObject("Select", 1);
26
 
27
	//test it
28
	$this->assertEqual($edited, $person->FirstName);
29
 
30
	//change it back
31
	$person->FirstName = $expect;
32
	TMapper::instance()->update("Update", $person);
33
}
34
 
35
function testPersonDelete()
36
{
37
	//insert it
38
	$person = new Person;
39
	$person->ID = -1;
40
	TMapper::instance()->insert("Insert", $person);
41
 
42
	//delte it
43
	$count = TMapper::instance()->delete("Delete", -1);
44
	$this->assertEqual(1, $count);
45
}
46
</com:TTextHighlighter>
47
 
48
<p>Not the best tests ever written, but for now, they will do :)</p>
49
 
50
<p>To make the new tests work, we'll need some new mapping statements.
51
The following sample shows the complete mapper document that we've called
52
<tt>personHelper.xml</tt>.</p>
53
 
54
<com:TTextHighlighter Language="xml" CssClass="source">
55
<?xml version="1.0" encoding="utf-8" ?>
56
 
57
<sqlMap Name="PersonHelper">
58
  <select id="Select" parameterClass="int" resultClass="Person">
59
   select
60
    PER_ID as ID,
61
    PER_FIRST_NAME as FirstName,
62
    PER_LAST_NAME as LastName,
63
    PER_BIRTH_DATE as BirthDate,
64
    PER_WEIGHT_KG as WeightInKilograms,
65
    PER_HEIGHT_M as HeightInMeters
66
    from PERSON
67
    WHERE
68
      PER_ID = #value#
69
  </select>
70
 
71
  <insert id="Insert" parameterClass="Person">
72
   insert into PERSON
73
    (PER_ID, PER_FIRST_NAME, PER_LAST_NAME,
74
    PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M)
75
   values
76
    (#ID#, #FirstName#, #LastName#,
77
    #BirthDate#, #WeightInKilograms#, #HeightInMeters#)
78
  </insert>
79
 
80
  <update id="Update" parameterClass="Person">
81
   update PERSON set
82
    PER_FIRST_NAME = #FirstName#,
83
    PER_LAST_NAME = #LastName#,
84
    PER_BIRTH_DATE = #BirthDate#,
85
    PER_WEIGHT_KG = #WeightInKilograms#,
86
    PER_HEIGHT_M = #HeightInMeters#
87
   where PER_ID = #ID#
88
  </update>
89
 
90
  <delete id="Delete" parameterClass="int">
91
   delete from PERSON
92
   where PER_ID = #value#
93
  </delete>
94
</sqlMap>
95
</com:TTextHighlighter>
96
 
97
<p>Well, waddya know, if run our tests now, we are favored with a green bar!. It
98
all works!</p>
99
 
100
<div class="note"><b class="tip">Note:</b>
101
Though, of course, things usually do not work perfectly the first time! We
102
have to fix this and that, and try, try, again. But SimpleTest makes trying
103
again quick and easy. You can changes to the XML mapping documents and rerun
104
the tests! No muss, no fuss.
105
</div>
106
 
107
<p>Turning back to our Prado page, we can revamp the <tt>TDataGrid</tt> to allow in-place
108
editing and deleting. To add records, we provide a button after the grid that
109
inserts a blank person for client to edit. The page code is shown as:
110
 
111
<com:TTextHighlighter Language="prado" CssClass="source">
112
    &lt;com:TDataGrid id="personList"
113
            DataKeyField="ID"
114
            AutoGenerateColumns="False"
115
            OnEditCommand="editPerson"
116
            OnUpdateCommand="updatePerson"
117
            OnCancelCommand="refreshList"
118
            OnDeleteCommand="deletePerson">
119
        &lt;com:TBoundColumn DataField="FirstName" HeaderText="First Name" />
120
        &lt;com:TBoundColumn DataField="LastName" HeaderText="Last Name" />
121
        &lt;com:TBoundColumn DataField="HeightInMeters" HeaderText="Height" />
122
        &lt;com:TBoundColumn DataField="WeightInKilograms" HeaderText="Weight" />
123
        &lt;com:TEditCommandColumn
124
                HeaderText="Edit"
125
                UpdateText="Save" />
126
        &lt;com:TButtonColumn
127
                HeaderText="Delete"
128
                Text="Delete"
129
                CommandName="delete"/>
130
    &lt;/com:TDataGrid>
131
    &lt;com:TButton Text="Add" OnClick="addNewPerson" />
132
</com:TTextHighlighter>
133
 
134
<p>The following sample shows the corresponding methods from page PHP class.</p>
135
 
136
<com:TTextHighlighter Language="php" CssClass="source">
137
    private function sqlmap()
138
    {
139
        return $this->Application->getModule('SQLMap')->getClient();
140
    }
141
 
142
    private function loadData()
143
    {
144
        $this->personList->DataSource =
145
                $this->sqlmap()->queryForList('SelectAll');
146
        $this->personList->dataBind();
147
    }
148
 
149
    public function onLoad($param)
150
    {
151
        if(!$this->IsPostBack)
152
            $this->loadData();
153
    }
154
 
155
    protected function editPerson($sender,$param)
156
    {
157
        $this->personList->EditItemIndex=$param->Item->ItemIndex;
158
        $this->loadData();
159
    }
160
 
161
    protected function deletePerson($sender, $param)
162
    {
163
        $id = $this->getKey($sender, $param);
164
        $this->sqlmap()->update("Delete", $id);
165
        $this->loadData();
166
    }
167
 
168
    protected function updatePerson($sender, $param)
169
    {
170
        $person = new Person();
171
        $person->FirstName = $this->getText($param, 0);
172
        $person->LastName = $this->getText($param, 1);
173
        $person->HeightInMeters = $this->getText($param, 2);
174
        $person->WeightInKilograms = $this->getText($param, 3);
175
        $person->ID = $this->getKey($sender, $param);
176
        $this->sqlmap()->update("Update", $person);
177
        $this->refreshList($sender, $param);
178
    }
179
 
180
    protected function addNewPerson($sender, $param)
181
    {
182
        $person = new Person;
183
        $person->FirstName = "-- New Person --";
184
        $this->sqlmap()->insert("Insert", $person);
185
        $this->loadData();;
186
    }
187
 
188
    protected function refreshList($sender, $param)
189
    {
190
        $this->personList->EditItemIndex=-1;
191
        $this->loadData();
192
    }
193
 
194
    private function getText($param, $index)
195
    {
196
        $item = $param->Item;
197
        return $item->Cells[$index]->Controls[0]->Text;
198
    }
199
 
200
    private function getKey($sender, $param)
201
    {
202
        return $sender->DataKeys[$param->Item->DataSourceIndex];
203
    }
204
</com:TTextHighlighter>
205
 
206
<p>OK, we are CRUD complete! There's more we could do here. In particular, we
207
should add validation methods to prevent client from entering alphabetic
208
characters where only numbers can live. But, that's a different Prado
209
tutorial, and this is an SQLMap DataMapper tutorial.</p>
210
 
211
<img src=<%~ grid2.png %> class="figure" />
212
<div class="caption"><b>Figure 4:</b> Person List CRUD</div>
213
 
214
</com:TContent>