You do not need to define your own Table_Row. However, it maybe useful in many cases, particularly if you want to define some specific methods or properties for a given user row. They can also improve readability of your code.
For example in your Users table case, you could define a method called getFullName() in a custom user row as:
public function getFullName() {
return $this->firstName . ' ' . $this->lastName;
}
Then when you obtain user row object, to get the full name of the user, you just do:
$user = $uTable->find($newUid)->current();
$fullName = $user->getFullName();
Second example is when you have some parent table to the Users table, such as Addresses. In this case you could define a method called getAddress in a user row:
public function getAddress() {
return $this->findParentRow('Application_Model_DbTable_Addresses');
}
In this scenario, you would get an Address row object for a current user as follows:
$user = $uTable->find($newUid)->current();
$addressRow = $user->getAddress();
Another example, would be when you want to create custom delete or instert methods. Lets assume that you want to make sure you do not want to delete an admin user using delete() method. Then you could overload delete method from Zend_Db_Table_Row as follows:
public function delete() {
if ('admin' === $this->userRole) {
return 0;
}
return parent::delete();
}
This way, you would not be able to delete an admin user just by calling delete() on a user row object:
$user = $uTable->find($newUid)->current();
$rowsDeleted = $user->delete(); // would be 0 if $user is admin
These are just three basic examples showing usefulness of defining your own row classes. But of course they are not necessary. However, from my own experience they are quite handy.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…