1

I have written two functions like below in my model class.

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

But I want to write the fetch function in my row table class and access those values from model class without writing the above two function. Please help me in this regard.

Thanks

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

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

And call it like

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

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

OR With out the Model function

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

With out Model and Using AND //for username and phone are equal

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

update as per OP's request

After fetching like this $result = $obj->fetchRow($where);

You can get the individual elements like these

$result->phone;

Or

$result->username;
Sign up to request clarification or add additional context in comments.

4 Comments

public function fetch($userId) { $select = $this->select(); $select->where('userId = ?', (int) $userId); $select->where('active = ?', 'Y'); return $this->fetchRow($select); } if I write my fetch function above one and try to access phone or username when ever it is required by userId
thansks, but I have another problem. :( I want to write the fetch in my row.php that is row class. But your code works if I write it in users.php that is db class
Edit your question with the contents of the file row.php
Sorry for giving wrong information. the class is User.php I update my question
0

You can just write a public function fetchRow($selectObjet) function in your table class (Application_Model_DbTable_TableName). That will override the native fetchRow method. You can also get the default result by calling parent::fetchRow in that function and modify the result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.