Later in this section: Introduction to creating a data layer with Zend Framework
This article contains a few examples on how to create and use data layer classes that was created by extending the Zend_Db_Table class from Zend Framework.
Let’s assume that we have a database table, called “usersâ€. This table contains three fields: id, which is table’s primary key; login, which is a text field with alphanumeric characters only; and password, which cannot be empty.
Our first approximation to a fully-functional class will be a simple subclass of Zend_Db_Table:
class Users extends Zend_Db_Table {
}
Zend’s database object mapping converts database names to class/property names by switching names from C-style to camel/Pascal style. E.g. the “users†is mapped by “Users†class, the “user_login†field is mapped by userLogin property. So when you create a subclass of Zend_Db_Table it will be automatically associated with the database object with corresponding name.
However, using direct mapping of objects may cause a naming conflict between database and other classes. It is recommended to add a database prefix to each class that represents a database object. In other words, it is better to create database classes only whithin one particular namespace.
Zend_Db_Table has a special class variable, called $_name, that specifies the name of the associated table. So when the new database class is created, the actual table name should be saved into this variable:
class DbUsers extends Zend_Db_Table {
protected $_name = 'users';
}
Even with this very simple class you can perform common database operations (such as insert, update, delete) in the following manner:
Create new user
// creating data object $users = new DbUsers(); // creating a new record--an object of Zend_Db_Row class $user = $users->createNew(); // set fields of the new record $user->login = 'Alexander'; $user->password = '*****'; // saving it into the database $user->save();
Select user with id=10 and change user’s password
// fetching a record--an object of Zend_Db_Row class $user = $users->find(10); // change user's password $user->password = '*****'; $user->save();
Commonly, the data layer classes validate data records before sending them to the database. For this we will add the validation code to the update operation and define the special type of exception, which will indicate the validation error. In DBMS terms we are about to add a trigger to table’s update operation.
// Defining an exception
class InvalidDbDataException extends Exception
{}
// DbUsers class with a "trigger" on update operation
class DbUsers extends Zend_Db_Table {
protected $_name = 'users';
// this function is being executed during
// saving a record into the database
function update($params, $where)
{
// Validate a password field and throw
// InvalidDbDataException exception when it is empty
if ('' == $params['password']) {
throw new InvalidDbDataException('The password field cannot be empty');
}
return parent::update($params, $where);
}
}
// Fetch the record, replace the password field’s value
// with an empty value and save the record.
$user = $users->find(2);
$row->password = '';
// Catch the exception, which would be thrown during saving,
// and show exception’s message
try {
$row->save();
} catch (InvalidDbDataException $e) {
echo 'Updating fialed. Reason: ' . $e->getMessage();
}