在Zend Framework中,使用多个表关系建模对象可以通过以下方法实现:
class Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
protected $_dependentTables = array('User_Profiles');
protected $_referenceMap = array(
'User_Profiles' => array(
'columns' => 'user_id',
'refTableClass' => 'User_Profiles',
'refColumns' => 'user_id'
)
);
}
class User_Profiles extends Zend_Db_Table_Abstract
{
protected $_name = 'user_profiles';
protected $_referenceMap = array(
'Users' => array(
'columns' => 'user_id',
'refTableClass' => 'Users',
'refColumns' => 'user_id'
)
);
}
$user = new Users();
$userRow = $user->fetchRow($user->select()->where('id = ?', $userId));
$userProfile = $userRow->findDependentRowset('User_Profiles');
class Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
protected $_dependentTables = array('User_Profiles');
protected $_referenceMap = array(
'User_Profiles' => array(
'columns' => 'user_id',
'refTableClass' => 'User_Profiles',
'refColumns' => 'user_id'
)
);
public function findUserWithProfile($userId)
{
$userRow = $this->fetchRow($this->select()->where('id = ?', $userId));
$userProfile = $userRow->findDependentRowset('User_Profiles');
return array('user' => $userRow->toArray(), 'profile' => $userProfile->toArray());
}
}
这样,在控制器中就可以使用Users表对象的findUserWithProfile方法来获取用户和用户档案的关联数据。
领取专属 10元无门槛券
手把手带您无忧上云