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