2013-03-28 37 views
0

我已經編寫代碼來搜索名字和姓氏,並顯示這些特定記錄(如果可用)並執行諸如更新和刪除這些記錄等操作,我已經編寫了以下代碼來搜索請告訴我什麼是建立。我正在以下錯誤搜索控制器正確的做法:在Zend框架中搜索一組記錄

Message: Method "select" does not exist and was not trapped in __call() 

我已經寫在控制器中的代碼是:

public function searchAction($params) 
{ 
    $query = $this->select() 
       ->from(
       array('EMPLOYEES'), 
       array('FIRST_NAME','LAST_NAME','SALARY','HIREDATE') 
       ); 
$query = $this->_makeParams($query,$params); 
return $this->fetchAll($query); 
} 

private function _makeParams($query, $params) 
{ 
    $firstname = isset($params['firstname']) ? trim($params['firstname']) : ''; 
    $lastname = isset($params['lastname']) ? trim($params['lastname']) : ''; 
$salary = isset($params['salary']) ? trim($params['salary']) : ''; 
$hiredate= isset($params['hiredate']) ? trim($params['hiredate']) : ''; 

if($firstname!='') 
{ 
    $name = '%'.$this->quote($firstname).'%';//quote is my own function 
    $query->where("EMPLOYEES.FIRST_NAME LIKE '?'",$firstname); 
    } 

if($lastname!='') 
{ 
    $query->where("EMPLOYEES.LAST_NAME =?",$lastname); 
} 

if($salary!='') 
{ 
    $query->where("EMPLOYEES.SALARY=?",$salary); 
} 

if($hiredate!='') 
{ 
    $query->where("EMPLOYEES.HIRE_DATE=?",$hiredate); 
} 
} 

回答

1

您的錯誤來自您調用select()而不是數據庫對象的控制器對象:

public function searchAction($params) 
{ 
    //$this in this context is a Zend_Controller_Action object 
    //you need to query against your database object. 
    $db = Zend_Db_Table::getDefaultAdapter(); 
    $query = $db->select() 
       ->from(
       array('EMPLOYEES'), 
       array('FIRST_NAME','LAST_NAME','SALARY','HIREDATE') 
       ); 
$query = $this->_makeParams($query,$params); 
//again make sure to query against the database object 
return $db->fetchAll($query); 
} 

,如果你沒有一個數據庫對象在bootstrap.php中創建或的application.ini您可以創建一個與Zend_Db的:

$db = Zend_Db::factory('Pdo_Mysql', array(
    'host'  => '127.0.0.1', 
    'username' => 'webuser', 
    'password' => 'xxxxxxxx', 
    'dbname' => 'test' 
)); 
+0

我已經在application.ini中完成了以下操作:resources.db.adapter =「pdo_mysql」 resources.db.params.host =「localhost」 resources.db.params.username =「根」 resources.db.params.password = 「」 resources.db.params.dbname = 「廣場」 resources.db.params.charset = 「utf-8」 resources.db.isDefaultTableAdapter =真 – user2176313 2013-03-28 10:10:09

+0

更新,以反映您的用例。根據指示添加$ db變量以訪問默認的數據庫適配器。這假定您只有一個適配器設置。 – RockyFord 2013-03-28 10:42:13

+1

我的建議是,您將學習如何使用模型與數據庫交互。一旦開發出適合您的模型結構,您會發現使用數據庫更容易,並且您的控制器將更加簡單。 – RockyFord 2013-03-28 10:44:36

1
public function searchAction($params) 
{ 
    $db  = Zend_Registry :: get('db'); 
    $select = $db->select(); 
    $query = $select->from(
       array('EMPLOYEES'), 
       array('FIRST_NAME','LAST_NAME','SALARY','HIREDATE') 
       ); 
$query = $this->_makeParams($query,$params); 
return $this->fetchAll($query); 
} 

您正在收到錯誤,因爲您尚未創建Zend registry的對象。請複製該函數並替換您的代碼

+0

它給這樣的消息:消息:沒有進入已註冊f或鍵'db' – user2176313 2013-03-28 09:19:32