0

我已经编写了代码来搜索名字和姓氏并显示这些特定记录(如果可用)并对这些记录执行更新和删除等操作,我编写了以下代码进行搜索,请告诉我构建的正确方法是什么搜索控制器。我收到以下错误:

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

2 回答 2

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 注册表的对象。所以复制该函数并替换您的代码

于 2013-03-28T09:14:14.033 回答
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'
));
于 2013-03-28T09:49:19.353 回答