Zend framework 如何在zend框架中编写行表类?

Zend framework 如何在zend框架中编写行表类?,zend-framework,zend-db,Zend Framework,Zend Db,在我的模型类中,我编写了如下两个函数 public function fetchByUsername($username) { $select=$this->select(); $select->where('username = ?', $username) ; $user = $this->fetchRow($select); return $user;

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

 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;
        }
但我想在我的row table类中编写fetch函数,并从模型类中访问这些值,而无需编写上述两个函数。 请在这方面帮助我

谢谢

User.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;
    // }
}
Users.php

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

    protected $_saveInsertDate  = true;
    protected $_saveUpdateDate  = true;
    }
UsersController.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;
        }
    }

您只需在表类(
Application\u Model\u DbTable\u TableName
中编写一个
公共函数fetchRow($selectObjet)
函数即可。这将覆盖本机
fetchRow
方法。您还可以通过在该函数中调用
parent::fetchRow
并修改结果来获得默认结果。

您只需在表类(
Application\u Model\u DbTable\u TableName
中编写一个
公共函数fetchRow($selectObjet)
即可。这将覆盖本机
fetchRow
方法。您还可以通过在该函数中调用
parent::fetchRow
并修改结果来获得默认结果

    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);
没有型号,用户名和电话的使用和//是相等的

  $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;

就这样说吧

 //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);
没有型号,用户名和电话的使用和//是相等的

  $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;


公共函数fetch($userId){$select=$this->select();$select->where('userId=?',(int)$userId);$select->where('active=?','Y');返回$this->fetchRow($select);}如果我在一个函数上面写fetch函数,并尝试在userIdthansks需要时访问phone或username,但我还有另一个问题:(我想在我的row.php(这是row类)中编写fetch。但是如果我在users.php(这是db类)中编写代码,那么您的代码就可以工作。使用文件row.phpSorry的内容编辑您的问题,以提供错误的信息。该类是User.php我更新我的questionpublic函数fetch($userId){$select=$this->select();$select->where('userId=?',(int)$userId);$select->where('active=?','Y');返回$this->fetchRow($select);}如果我在一个函数上面写我的fetch函数,并在userIdthansks需要时尝试访问phone或username,但我有另一个问题:(我想在我的row.php(即row类)中编写fetch。但是如果我在users.php(即db类)中编写代码,那么您的代码可以工作。请使用文件row.phpSorry的内容编辑您的问题,以避免提供错误的信息。该类为User.php我更新我的问题