Sunday, July 3, 2011

Easy PHP Websites with the Zend Framework Part 6

Chapter 6 of the book provides an excellent introduction to working with database tables in your applications using Zend_Db. In order to follow the examples in the book you need a database with a populated "games" table, but the examples in the book rely on a simpler "games" table than the one seeded with the "seed.php" script.

The following SQL statements can be used to create and seed a table that can be used to follow the examples in the book:

CREATE TABLE IF NOT EXISTS `games` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`asin` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`publisher` varchar(255) NOT NULL,
`rel` date NOT NULL,
`price` decimal(5,2) NOT NULL
);

INSERT INTO `games` (`id`, `asin`, `name`, `publisher`, `rel`, `price`)
VALUES
(1, 'B00269QLI8', 'Call of Duty: Modern Warfare 2', 'Activision Inc.', '2009-11-10', '49.99'),
(2, 'B002I08CJG', 'Call of Duty: Modern Warfare: Reflex', 'Activision Inc.', '2009-11-10', '19.99'),
(3, 'B0016B28Y8', 'Call of Duty 4 : Modern Warfare', 'Activision Inc.', '2008-04-01', '29.99'),
(4, 'B000TG530M', 'Call of Duty 4: Modern Warfare', 'Activision Inc.', '2007-11-05', '59.99'),
(5, 'B001TOQ8PM', 'Call of Duty: Modern Warfare 2 Hardened Edition', 'Activision Inc.', '2009-11-10', '79.99'),
(6, 'B002BSA388', 'Super Mario Galaxy 2', 'Nintendo', '2010-05-23', '49.99'),
(7, 'B002BRZ9G0', 'New Super Mario Bros. Wii', 'Nintendo', '2009-11-15', '49.99'),
(8, 'B000XJNTNS', 'Mario Kart Wii with Wii Wheel', 'Nintendo', '2008-04-27', '49.99'),
(9, 'B000FQ9QVI', 'Super Mario Galaxy', 'Nintendo', '2007-11-12', '49.99'),
(10, 'B000LSJKAM', 'Mario Party 8', 'Nintendo', '2007-05-29', '49.99')


You can now add the example queries to the indexAction of the IndexController to learn Zend_Db by using it.

3 comments:

Frank Drouillard said...

To use the indexAction of the IndexController, the code on page 92 for "Retrieving Multiple Rows" must be revised to read:

$gameTable = new Application_Model_Dbtable_Games();
$query = $gameTable->select();
$query->where('price > ?', 49.99);
$results = $gameTable->fetchAll($query);
foreach($results AS $result) {
echo "{$result->name} (ASIN: {$result->asin})
";
}

to avoid throwing an application error that complains about the fetchAll() method.

Steve said...

I am having a bit of an issue with chapter 7. There seem to be some problem with the models for Rank and Accounts containing the same code. This makes the command ./scripts/doctrine orm:schema-tool:create

Any comment on this?

Jonathan said...

The Rank model is wrong and missing. You need to delete it for ./scripts/doctrine orm:schema-tool:create to work. Of course your application.ini needs to be correct too.