1

モデルクラスに以下のような2つの関数を記述しました。

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

しかし、行テーブルクラスにフェッチ関数を記述し、上記の2つの関数を記述せずに、モデルクラスからそれらの値にアクセスしたいと思います。この点で私を助けてください。

ありがとう

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;
        }
    }
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 に答える