The folks at Zend have released version 0.1.1 of the Zend FrameworkAndi Gutman’s annoucement also mentions the new Zend Developer Zone, which seems to be starting off with some high quality articles – including one on connecting PHP with Macromedia Flex.

I downloaded the new framework, but haven’t had much time to look through all the docs or really get going with it in earnest. From first glance, it seems to be closer to a component library right now – there aren’t any full examples that go through using it as a true ‘framework’. That being said, it looks like most of the building blocks are there.

On the database access side of things, it’s actually quite confusing now – the documentations refers to Zend_Db_DataObject, which seems to be the Active Record implementation seen in the php|archictect webcast, but the code doesn’t exist with the framework. Andi clarified that this code doesn’t exist and the docs were accidentally included – it’s not clear, however, whether this will eventually be added or not. Zend_Db_Table seems similar in some ways, but is not as straightforward as the Active Record approach. I was able to put together a quick example of pulling data from a database, and here’s what it looked like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
require_once 'Zend/Db.php';
$params = array (
    'host'     => 'localhost',
    'username' => 'test',
    'password' => '********',
    'dbname'   => 'delicious'
);
 
$db = Zend_Db::factory('pdo Mysql', $params);
 
require_once 'Zend/Db/Table.php';
Zend_Db_Table::setDefaultAdapter($db);
 
class Posts extends Zend_Db_Table {}
 
$table = new Posts();
$posts = $table->fetchAll(null, null, 10);
foreach($posts as $post) {
	?>
	<a href="<?=$post->href;?>"><?=$post->description;?></a>
	(Posted <?=Date("m/d/y",$post->posted);?>)<br>
	<?
}
 
Zend::dump($db->describeTable('posts'));
?>

In this example, I create a new PDO mySQL adapter (line 10), passing in the database connection properties. Set this database adapter to be the default one used for Zend_Db_Table (line 13). Create a new class that extends Zend_Db_Table (line 15) – the name of the class is the same as the DB table name, and this is all Zend_Db_Table needs. Lines 17 and 18 get an instance of this class, and retrieve the first 10 rows from this table. Lines 19-24 print various columns from the returned rows out on the screen. Lastly, on line 26 I print out a description of the table, which is an array that lists the column names, their types, length, etc.

All in all, it seems pretty easy to use, and it also seems easy to add and update rows. What’s missing from the capabilities included in the Ruby on Rails implementation of Active Record is the ability to create relationships between tables – this is a truly great feature that simplifies the life of a developer quite a bit, and I hope that Zend can evolve in this direction.

Another person who has obvious checked this stuff out is John Lim, creator of ADODB, the finest DB abstraction library for PHP. John put together a quick Active Record implementation using ADODB. The big benefits of this implementation is that it works in both PHP4 and PHP5 (Zend framework is PHP5 only and the DB stuff on works well with PDO), and its running on top of the very well tested ADODB library that already has support for just about every database under the sun.

Hopefully the continued innovation and competition will lead to better tools that are easier to use!