1

我在我的模型类中编写了如下两个函数。

 public function fetchByUsername($username)
        {
            $select=$this->select();
            $select->where('username = ?', $username) ;
            $user = $this->fetchRow($select);
            return $user;
        }

    public function fetchByPhone($phone)
        {
            $select = $this->select();
            $select->where('phone = ?', $phone) ;
            $user = $this->fetchRow($select);
            return $user;
        }

但是我想在我的行表类中编写 fetch 函数并从模型类中访问这些值而不编写上述两个函数。请在这方面帮助我。

谢谢

用户.php

class Model_User extends Model_DbTable_Row
{
    //here i need to write fetch function like below
    // public function fetchPhone()
    // {
    //    return $this->phone;
    // }

    // public function fetchUsername()
    // {
    //    return $this->username;
    // }
}

用户.php

class Model_Users extends Model_DbTable_Abstract
{
    protected $_name     = 'users';
    protected $_primary  = 'userId';
    protected $_rowClass = 'Model_User';

    protected $_saveInsertDate  = true;
    protected $_saveUpdateDate  = true;
    }

用户控制器.php

class Admin_UsersController extends BusinessForum_Admin_Controller
{

    public function init()
    {
        /* Initialize action controller here */
        $this->view->pageTitle = 'Users - ' . $this->view->pageTitle;
        parent::init();
    }

    public function indexAction()
    {   
        // get name of the director
        $userId = $this->_getParam('directorId');
        if ($userId) {
        $modelUsers = new Model_Users();
        $user = $modelUsers->fetch($userId);
        $fullName = $user->firstName." ".$user->lastName;
        $directorName = "Direcotor : ".$fullName;
        $this->view->directorName = $directorName;
        }
    }
4

2 回答 2

1
    public function fetchDetails($attr,$value)
        {
            $select=$this->select();
            if($attr == 'username'){
            $select->where('username = ?', $value) ;
            else if($attr == 'phone '){
             $select->where('phone = ?', $value) ; 
            }
            $user = $this->fetchRow($select);
            return $user;
        }

并称它为

 //user
    $details = $your_model->fetchDetails('username',$usernamedata);

    //phone
    $details = $your_model->fetchDetails('phone',$phonedata);

或没有模型功能

 $obj = new Yournamespace_Model_Yourfile();
 $where = $obj->getAdapter()->quoteInto('username = ?',$username);
 $result = $obj->fetchRow($where);

没有模型和使用 AND //对于用户名和电话是相等的

  $where = array();
     $obj = new Yournamespace_Model_Yourfile();
     $where[] = $obj->getAdapter()->quoteInto('username = ?',$username);
     $where[] = $obj->getAdapter()->quoteInto('phone = ?',$phone);
     $result = $obj->fetchRow($where);
    //if you have more than one row then use
    $result = $obj->fetchAll($where);

根据OP的要求更新

像这样获取之后 $result = $obj->fetchRow($where);

你可以得到像这样的单个元素

$result->phone;

或者

$result->username;
于 2012-11-19T08:16:25.680 回答
0

您可以public function fetchRow($selectObjet)在表类 ( Application_Model_DbTable_TableName) 中编写一个函数。这将覆盖本机fetchRow方法。您还可以通过调用parent::fetchRow该函数并修改结果来获得默认结果。

于 2012-11-19T07:06:29.107 回答