Simplify PHP Development with WASP
Pages: 1, 2, 3, 4, 5
In order to fill the arTasks placeholder, you need an array of task records that exist in the database. You have access to this data through WASP's db model layer, using the Wrapper classes that you generated by running phing db and located in the Todo/db/ directory.
The code:
$oTasks = new TaskWrapper();
$oTasks->findAll();
constructs a DataObjectWrapper object for the Task table, and finds all of the existing records.
Now you can loop over the values with the code:
while ($oTasks->next())
The next() method will return false when there are no more records.
While looping through the set of tasks, go ahead and add them to an array to pass to the view:
$arTasks[$oTasks->getId()] = $oTasks->toArray();
This code uses the built-in method getId() to index the array using the primary key of the table you're working with. Referring back to the database-create SQL, this gives the value of the column TaskId. At that index, set the value of the array to the array value of the Wrapper object. The built-in toArray() method returns an array representation of the column data in the table. For example, the array might look something like this:
{ 'TaskId' = '1', 'Name' => 'Buy Groceries'}
The view code doesn't care about the TaskId field, but if you remember, the line:
{task[Name]}
uses the Name field to display the name of the task on the page.
As there isn't a form to process on this page, you don't need a handleEvents() method this time.
Now that you have the code written for the front page of the task list, consider what happens when the page loads. If you haven't saved any task items, there won't be any entries in $arTasks. Because the Flexy placeholder arTasks is null, the block:
<li flexy:foreach="arTasks,key,task">{task[Name]} - <i>{task[Due]}</i></li>
will not display.
Once you go to the Todo/Entry/ page and create the first task, the flexy:foreach has a value to loop over, and the list item will display, as Figure 3 shows.

Figure 3. Showing one task
As you create more entries, Flexy automatically displays more list items (Figure 4).

Figure 4. Showing multiple list items
Finishing Touches
Congratulations! You now have a functioning task list where you can go and post items for anyone to see. Clearly, however, you have some work to do in the way of tailoring the design to your own style, and adding features such as completion marking and category grouping. Fortunately the HTML is all in the view layer, where you can create and modify templates without fussing over embedded PHP code.
Conclusion
Tools such as DB_DataObjects and Flexy have been available for use in PHP for a while, but few projects have put them together in such a unified way. With the enhanced object-oriented nature of PHP 5, working in three tiers with PHP using WASP is easier. Hopefully this tutorial has provided all of the information you need to go out and create feature-rich dynamic web applications in WASP. For more information on creating applications in WASP, see the WASP documentation.
Return to the PHP DevCenter.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 3 of 3.
-
A work in progress
2007-01-17 19:56:55 ricksta [Reply | View]
The integration of my 2 favorite php components - Flexy templates and Pear DataObjects - seemed to good to be true, and like most frameworks, WASP needs refinement.
WASP can only begin to simplify web development once it has been installed, configured, and learned - and currently that is difficult. The automated configuration is great if it works, but what to do if not? We can find some documentation on Flexy and DataOjects but the documentation on WASP is limited to the api which doesn't explain how the classes work together. An event-driven architecture is great, but with one catchall method you still have to apply logic to determine which code to execute. Prado and Code Igniter solve this by using a seperate methods for different events - not to say those frameworks are perfect.
-
Can't make it work
2006-06-25 21:23:30 Acharn [Reply | View]
I'm using WASP 1.1.2, and it seems very good indeed, except I can't get the code for the main index.php page to work. It breaks when I try to create the new TaskWrapper.
I'm frustrated by the apparent lack of any debugging tools. I use PHP Designer 2006 as my editor and it has a debugging mode which indicated that PHP doesn't know how to build the new object. I wonder if there shouldn't be a 'require_once' somewhere in the file.
A couple of other things that bother me: in the article the file is named ToDoMainIndexPate.php, but the page that WASP created for me is named ToDoIndexPage.php; when I created the project, the build.properties file contained two entries for the "Directory where PEAR packages are installed", one of them incorrect (I deleted those lines).
I'm still playing with this, hoping to understand it, because it would be enormously useful to me if I could use it, but the project that is my goal is quite complex, uses a database with many interlinked tables, and I can't figure out enough about how the different pieces WASP creates are interacting with each other.



phing -buildfile /Library/php5/lib/php/data/WASP/build.xml wasp-project
I get the following error:
Warning: require_once(phing/Project.php): failed to open stream: No such file or directory in /Library/PHP5/lib/php/phing/Phing.php on line 22
Fatal error: require_once(): Failed opening required 'phing/Project.php' (include_path='.:') in /Library/PHP5/lib/php/phing/Phing.php on line 22
I'm sure something isn't looking in the right place?
Thanks