Getting started with REST using Zend Framework

Creating a REST server in ZendFramework can be simple as 3 lines of code. For the sake of simplicity, let’s suppose we’ll place all of our code logic in one file, the IndexController.php. The basic building block of each and every Zend Framework application. Code for creating a REST server looks like

public function restAction()
{
$server = new Zend_Rest_Server();
$server->setClass(‘CustomTestClass’);
$server->handle();
exit;
}

Notice the CustomTestClass? This is the name of the class we wish to make availabe trough REST server. For the sake of simplicity, this class is placed in same file as our IndexController class. Below is the sample of class body

class CustomTestClass
{
/**
* Write to a file
*
* @param string $string
* @return string Some return message
*/
public function sayHello($name)
{
$message = ‘Hello ‘.$name;
return $message;
}
}

Now all that’s left is to make a call to our REST server using GET method. Since our REST server code is placed inside restAction function in IndexController, it can be accessed using url like

http://localhost/myzendapp/index.php/index/rest

And the result should look like

< ?xml version=”1.0″ encoding=”UTF-8″?>

No Method Specified.

failed

Although these are error messages, it’s ok, it means our REST server is working. Now if we were to enter url like

http://localhost/myzendapp/index.php/index/rest?method=sayHello

we would get response error message like
< ?xml version=”1.0″ encoding=”UTF-8″?>

Invalid Method Call to sayHello. Requires 1, 0 given.

failed
This too is ok. It simply means we omited the parametars we were supose to pass to function. Remember, our sayHello function has signature like

public function sayHello($name)

meaning we should pass it a parametar $name. Now if we were to type into our browser url like

http://localhost/myzendapp/index.php/index/rest?method=sayHello&name=Branko

We would get response like

< ?xml version=”1.0″ encoding=”UTF-8″?>

Hello Branko
success
copied

MySQL transactions in Zend framework

Hi! Today I’m going to explain a part of Zend framework DB functionality. Transactions in general are quite useful, like temporary tables, but in most of situations unnecessary. Because of that, I’m going to explain when and how to use them through Zend framework.

What is transaction?
A simple answer would be – a number of individual queries that are grouped together.

An example would be something like this:

[php]
UPDATE balance SET total = total – 100 WHERE account_id=1;
UPDATE balance SET total = total + 100 WHERE account_id=2;
[/php]

Why transactions?
In general web development, transactions are unnecessary. If for some reason post in your CMS isn’t saved, you’ll write it again. It is a problem, but only annoying one, not a crucial one. But if you own a web shop, and someone create an order, pay for it, and you – owner of that store can’t see it because of database glitch, that would be a problem. Take a look at example above. I only first query in that transaction was executed, total amount of money from account would be decreased by 100, and that money would simply disappear (if we’re looking at your database). Or if only second was executed, account 2 would have 100 extra in its total sum – again virtually from nowhere.

This is the place where transactions are necessary. Because transactions either execute all queries assigned to it, or none of them. I hope you see the point.

The syntax
This is a basic example from official MySQL documentation:

[php]
START TRANSACTION;
–BEGIN TRANSACTION
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET [email protected] WHERE type=1;
–SET SOME QUERIES
COMMIT;
–AND COMMIT THEM
[/php]

When the Zend framework comes in
Its pretty much self explained, so here’s a code snippet with comments:

[php]
$db = self::db();
//GET DATABASE CONNECTION (DEPENDING ON YOUR SETUP)

$q = "UPDATE balance SET total = total – 100 WHERE account_id=1;UPDATE balance SET total = total + 100 WHERE account_id=2;";
//PREPARE QUERY

$db->beginTransaction();
//BEGIN TRANSACTION ON DATABASE CONNECTION
try
{
$db->exec($q);
//SET QUERY YOU WISH TO EXECUTE

$query = $db->commit();
//COMMIT QUERY TO DATABASE
}
catch(Exception $e)
{
$db->rollBack();
//ROLLBACK IF TRANSACTION FAILS

$error_result = Array();
$message = $e->getMessage();
$code = $e->getCode();
$error_result[0] = $message;
$error_result[1] = $code;
//GET ERROR INFORMATION IF SQL FAILS
}
[/php]

Conclusion
Now, You’ve created a transaction in your Zend project. You’ll get a rollback on your database if any of the queries fails and you won’t have any “missing records and values” in your database. This isn’t so complicated to create for every crucial query in your application, especially when you know its going to work like a charm!