我有两个表,一个用户表和一个应用程序表:
User
id
username
application
id
user_id
...
要获取应用程序,我希望能够通过应用程序id或用户名选择应用程序。使用
$row = $this->fetchRow("id = $id");
按app id获取应用程序非常简单,但我不确定如何按用户名获取应用程序。下面是我目前在模型中的相关代码。protected $_referenceMap = array (
'User' => array (
'columns' => 'user_id',
'refTableClass' => 'Application_Model_DbTable_User',
'refColumns' => 'id'
)
);
public function get_application($id) {
if(is_numeric($id)) {
$row = $this->fetchRow("id = $id");
} else {
//get application by username
}
return $row->toArray();
}
最佳答案
如果我正确理解你的问题,我想你只是想:
$userRow = array_shift($this->findDependentRowset('User'));
编辑:等等,不,这不对。一旦你得到了正确的应用程序,它就会找到用户。
假设你也有逆反关系,你应该能够做到:
$userTable = new Application_Model_DbTable_User();
$userRow = $userTable->fetchRow('username = ?' , $username); // or whatever
$applicationRow = $userRow->findParentRow('Application');