Michael Bommarito ([info]g0thm0g) wrote in [info]php,
@ 2005-08-09 10:41:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Current mood:wow

The Holy Grail of PHP
Thought I'd share this with the community. I'll be writing a real walkthrough soon, as this combo is too good to keep to myself.
Would love to hear any comments, criticisms, or alternatives. If anyone is interested in helping write a few pages on the topic, send me an email or something too.


What do you get when you combine an automated database object abstraction layer, an embedded templating engine, and an AJAX library?

Unparalled rapid modular development fusing the web's hottest technologies.
And it's easy.

PEAR DB_DataObject
PHP Smarty
xajax

What's it look like? Here's a teenie example.

index.php

<?php
require_once 'ajax.php';
?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<?php $xajax->printJavascript(); ?>
		<title>Test</title>
	</head>
	<body>
		<form id="frmNote">
			<div><b>ID</b>: <input type="text" name="id" id="id" /></div>
			<div><input type="button" 
        onclick="xajax_getNote(xajax.getFormValues('frmNote'))"
        value="Get Note" /></div>
		</form>
		<div id="content"></div>
	</body>
</html>



ajax.php
<?php
require_once 'xajax/xajax.inc.php';
require_once 'template.php';

function getNote($form) {
	$id = $form['id'];
	$v = new View();

	$objResponse = new xajaxResponse();
	$objResponse->addAssign("content","innerHTML", $v->getNote($id));

	return $objResponse->getXML();
}

$xajax = new xajax();

$xajax->registerFunction("getNote");
$xajax->processRequests();
?>



template.php
<?php
require_once 'Smarty/Smarty.class.php';
require_once 'data.php';

class View extends Smarty {
	function View() {
		parent::Smarty();
		$this->template_dir = 'tpl/';
		$this->compile_dir = 'tpl/comp';
		$this->config_dir = 'tpl/conf';
		$this->cache_dir = 'tpl/cache';
	}

	function getNote($id = null) {
		$note = DB_DataObject::Factory('notes');
		$note->get($id);
		$this->assign('note', $note);
		return $this->fetch('note.tpl');
	}
}
?>



tpl/note.tpl
<table>
	<tr>
		<td>{$note->name}</td>
	</tr>
	<tr>
		<td>{$note->body}</td>
	</tr>
</table>



data.php
<?php
require_once 'PEAR.php';
require_once 'DB/DataObject.php';

//Initialize configuration
$options = &PEAR::getStaticProperty('DB_DataObject','options');
$config = parse_ini_file('do.ini',TRUE);
$options = $config['DB_DataObject'];
?>



schema.sql
CREATE DATABASE organizer;
USE organizer;

CREATE TABLE notes (
	id INT UNSIGNED NOT NULL AUTO_INCREMENT,
	name VARCHAR(32),
	body TEXT,
	PRIMARY KEY(id)
);







(Post a new comment)


[info]wisn
2005-08-09 02:51 pm UTC (link)
I appreciate the post, but can you put all the code samples behind the cutline? It's whacking the hell out of my friend's list.

(Reply to this) (Thread)


[info]g0thm0g
2005-08-09 02:58 pm UTC (link)
How's that? Your friends page looks fine to me now.

What problem were you having? The whole point of the first code block was that you could get a feel for how simple the code ends up looking before committing to reading it all.

(Reply to this) (Parent)(Thread)


[info]wisn
2005-08-09 03:08 pm UTC (link)
I didn't have a problem with that, but one of the unwrapped lines was running off the window and making all the other text on the page bleed off too.

Back on topic, though: Thanks. I'll take a look at this tonight.

(Reply to this) (Parent)(Thread)


[info]sueposey
2008-08-11 06:53 pm UTC (link)
I didn`t have one then and I still do not but I do think there are new ways of helping with symptom management.

(Reply to this) (Parent)


(Anonymous)
2006-02-04 09:32 am UTC (link)
I could definantly see a use for this in Pagination.

(Reply to this) (Parent)

(Reply from suspended user)

[info]jaybigum
2008-08-11 05:15 pm UTC (link)
melissa // Jun at am You can put me down as uneducated, if I can put you down as a sexist jerk.

(Reply to this) (Parent)


[info]xinu
2005-08-09 02:59 pm UTC (link)
Thanks for sharing! I'll read through it more carefully when I get to work.

(Reply to this)


[info]coreb
2005-08-09 03:13 pm UTC (link)
So how does this compare to the 1 million+ Model-View-Controller Frameworks that have already been developed?

(Reply to this) (Thread)


[info]g0thm0g
2005-08-09 03:31 pm UTC (link)
Essentially, it's just another implementation of an MVC framework. Others might have more maturity in isolating components, but they're missing out on the whole AJAX revolution.

Besides, AJAX would allow the MVC model to be implemented on the web in a manner that simplifies the user's experience, just as it should've first been.

Ruby-on-Rails is exploding because of its native support for this sort of development, and it seems therefore an appropriate time to discuss and test implementations of similar frameworks for PHP.

(Reply to this) (Parent)(Thread)


[info]coreb
2005-08-09 03:56 pm UTC (link)
Yea, without ajax support an MVC framework has no chance at competing with ruby on rails, from what I am seeing.

What may be an interesting way to support your claim to this combination would be for you to implement a webapp your way and also in another framework as a comparison of ease of use.

A couple of the newer ones that I've found.
PHP on Trax
Biscuit
Jaws
Cake

(Reply to this) (Parent)


[info]bobalien
2005-08-10 10:39 pm UTC (link)
i never could get Ruby-on-Rails to work on my system and i'm just starting to get into working with AJAX and Object Relational DB stuff (I use ADOdb, never could get PEAR working right either - looks like i'll have to give it another whirl)

excellent post, you've always got the goods, i gotta get my hands dirty w/some of this stuff

(Reply to this) (Parent)(Thread)


[info]mileschantler
2008-08-11 12:15 pm UTC (link)
Yes, if you get to use rails, I get to use my object tool kit ; ) At the end of the day, writing maintainable code and making your users happy trumps any technological "advantage" a framework or new language has to offer.

(Reply to this) (Parent)


[info]bobalien
2005-08-12 12:23 am UTC (link)
what's your opinion on ADOdb vs. PEAR::DB? I'd like to start playing around with DB_DataObject, and I got PEAR::DB working; from what i've read ADOdb is faster and less buggy than PEAR; is it worth it for me to take a look at using PEAR's DB or should i try to find something comparable to DataObject that works with ADOdb (or something different entirely)

(Reply to this) (Parent)(Thread)


[info]g0thm0g
2005-08-12 01:03 am UTC (link)
ADOdb is more mature and has a C extension.

PEAR::DB has better design and a more active community.

In all honesty, I think PHP 5.5/6.0 is going to introduce a real contender in PDO. Keep your eyes on that, and if you've got a development machine handy, I'd suggest grabbing a build from snaps.php.net and getting your hands wet with PDO.

(Reply to this) (Parent)(Thread)

ADODB Vs Pear::DB Vs PDO
(Anonymous)
2005-09-01 11:13 pm UTC (link)
I use ADODB extensivly and find its great. Especially you can npw use it as a PHP extension... but, its clunky and not very lightweight. Pear::DB is slightly slower and not as nice in some areas, but great in others (Select meu generation is a great feature in ADODB, but also limited). To be honest I can't wait to try PDO, its looking really good and should be very fast. Just my useless 2c worth :) Great post by the way.

(Reply to this) (Parent)

xajax
(Anonymous)
2005-11-22 03:49 am UTC (link)
Hi Michael. This is the xajax project owner. Thanks so much for your interesting article. We're glad that you have found xajax to be useful. I thought I would let you know that we have been working hard on a new release of xajax. We plan to release a beta of the upcoming release later this week. Some of the new features include:

* New error handling: Non-fatal PHP errors can now be easily trapped and written to a log file or displayed in an alert dialog in the client.

* OOP: You can now register instance and static class methods to be callable through xajax. This is essential to make it easy to add xajax to many OOP CMS solutions.

* Internationalization: xajax defaults to use UTF-8, but we've added the ability to set the charset to whatever you need to.

* External Function Registration: you can now register functions in external .php files and xajax will dynamically include the file and call the function only if it is requested.

We will soon be setting up at xajaxproject.org, but for now you can visit our forums at:

http://www.intuitivefuture.com/xajaxforums/

I am interested in your feedback and your needs as you work with xajax. Feel free to ask questions and make feature requests. Thanks,

J. Max Wilson
xajax project owner

(Reply to this)

Mutreta das grossas...
(Anonymous)
2006-01-28 02:25 pm UTC (link)
Cara, isso ai é engembração, mutreta das grossa. hahaha

(Reply to this)

what should be in do.ini?
(Anonymous)
2006-06-12 04:25 pm UTC (link)
what should be write in do.ini? I got following message:

Warning: parse_ini_file(): Cannot open 'do.ini' for reading in d:\appserv\www\html\live\data.php on

(Reply to this) (Thread)

Re: what should be in do.ini?
(Anonymous)
2006-06-13 08:23 am UTC (link)
a quite stupid question I asked... since I never used dataobject @@

for someone who has the same question as me.

[DB_DataObject]
database = mysql://xxx:xxxx@localhost/xxxx
schema_location = classes
class_location = classes
require_prefix = /dataobjects/
class_prefix = DataObjects_

(Reply to this) (Parent)

undefined function: get()
(Anonymous)
2006-07-06 02:22 am UTC (link)
Thank you for your nice combination of scripts.

I am currently got a problem when I click GetNote button. It shows an error msg as below,

------------------------------------
Error: the XML response that was returned from the server is invalid.
Received:


Fatal error: Call to undefined function: get() in d:\appserv\www\live\template.php on line
16

You have whitespace in your response.
------------------------------------

line 16 is $note->get($id);
Any idea and suggestion are appreciated.

(Reply to this)

Alternative to DB:DataObject
(Anonymous)
2006-10-27 02:02 pm UTC (link)
Personally I live by phpobjectgenerator.com -- look at their tutorials for how to use it!

(Reply to this)


Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…