1

我有两个表,一个用户表和一个应用程序表:

User
    id
    username
application
    id
    user_id
    ...

要获取应用程序,我希望能够通过应用程序 ID 或用户名选择应用程序。通过应用程序 ID 获取应用程序很简单,$row = $this->fetchRow("id = $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();
}
4

1 回答 1

2

如果我正确理解你的问题,我想你只是想要:

$userRow = array_shift($this->findDependentRowset('User'));

编辑:等等,不,这是不对的。一旦你已经获得了正确的应用程序,它就会找到用户。

假设你也有逆向关系,你应该能够做到这一点:

$userTable = new Application_Model_DbTable_User();
$userRow = $userTable->fetchRow('username = ?' , $username); // or whatever
$applicationRow = $userRow->findParentRow('Application');
于 2012-06-13T18:28:16.710 回答