I\'m toying with Zend Framework and trying to use the \"QuickStart\" guide against a website I\'m making just to see how the process would work. Forgive me if this answer is
I would say that the best way to do this with ZF is by using Doctrine.
In a nutshell I would create a model for each table, not one model that accesses all three. I would then define relationships between the tables.
To be honest it seems not very "DRY" to have to create a model for each table but that is what I see done repeatedly in the various examples online and it is what I have done in the handful of projects that I have created with the Zend Framework. If anyone has a better way of handling this I hope the will post it here.
Basically instead of using Zend_Db_Table
use more general Zend_Db_Select
or Zend_Db_Statement
to retrieve data.
BTW. You might want to access password data not directly in User model, but rather in your User auth class derived from Zend_Auth_Adapter
.
class Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
protected $_rowClass = 'User';
protected $_dependentTables = array ('UserMetadata', 'UserPassword');
...
class UserMetadata extends Zend_Db_Table_Abstract
{
protected $_name = 'user_metadata';
protected $_referenceMap = array (
'Users'=> array (
'columns'=>'user_id',
'refTableClass'=>'Users',
'refColumns'=>'id'
)
);
...
class UserPassword extends Zend_Db_Table_Abstract
{
protected $_name = 'user_password';
protected $_referenceMap = array (
'Users'=> array (
'columns'=>'user_id',
'refTableClass'=>'Users',
'refColumns'=>'id'
)
);
Fetching data:
$id = //get your user id from somewhere
$users = new Users();
$user = $users->fetchRow('id=?', $id);
if ($user->authMethod == 0)
{
$metadata = $user->findDependentRowset('UserMetadata')->current();
}
or
$user = $users->fetchRow($users->select()
->where('gender=?, 'M')
->order('email ASC');
... etc.
Inserting data:
$newRow = $users->fetchNew();
$newRow->email = me@domain.com;
$newRow->save();
or
$users = new Users();
$data = array('email' => 'me@domain.com',
'firstname' => 'Me');
$users->insert($data);
Updating:
$user->email = 'me@domain.org';
$user->save();
Deleting a row:
$user->delete();
Using transaction:
$db->beginTransaction();
$db->commit();
$db->rollback();
etc... it's all in the ZF Manual!
Just a few notes straight off the bat:
The first User table, seems more like a Person table, except for the auth method, but you could put that in the User_Password table, which you could probably rename User. The User_Metadata table seems like it could just be amalgamated with the User_Password/User table.
Now, even without those changes, you have three distinct tables, with three distinct concepts, if you model each of those separate as different classes, and then had a UserModel class as a facade of sorts to access each, it would likely make it easier.