0

我需要在 zend frawork 中运行查询。查询如下所示。

select * from users where role_id in (3,5) and emailID not in ('abc@abc.com', 'cba@cba.com');

我正在传递字符串

$whereString = role_id in (3,5) and emailID not in ('abc@abc.com', 'cba@cba.com');

$this->fetchList($whereString) // where $this is object of users

但是 fetchList() 函数只执行第一部分,即role_id in (3,5)

但不是第二部分,即emailID not in ('abc@abc.com', 'cba@cba.com');

fetchList() 的结果包含 'abc@abc.com' 和 'cba@cba.com'

fetchList() 是:

    public function fetchList($where=null, $order=null, $count=null, $offset=null)
        {
                $resultSet = $this->getDbTable()->fetchAll($where, $order, $count, $offset);
                $entries   = array();
                foreach ($resultSet as $row)
                {
                        $entry = new Application_Model_Users();
                        $entry->setId($row->id)
                              ->setName($row->name)
                              ->setEmail($row->email)
                        $entries[] = $entry;
                }
                return $entries;
        }

获取所有():

   /**
     * Fetches all rows.
     *
     * Honors the Zend_Db_Adapter fetch mode.
     *
     * @param string|array|Zend_Db_Table_Select $where  OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
     * @param string|array                      $order  OPTIONAL An SQL ORDER clause.
     * @param int                               $count  OPTIONAL An SQL LIMIT count.
     * @param int                               $offset OPTIONAL An SQL LIMIT offset.
     * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
     */
    public function fetchAll($where = null, $order = null, $count = null, $offset = null)
    {
        if (!($where instanceof Zend_Db_Table_Select)) {
            $select = $this->select();

            if ($where !== null) {
                $this->_where($select, $where);
            }

            if ($order !== null) {
                $this->_order($select, $order);
            }

            if ($count !== null || $offset !== null) {
                $select->limit($count, $offset);
            }

        } else {
            $select = $where;
        }
        print_r($select);
        $rows = $this->_fetch($select);

        $data  = array(
            'table'    => $this,
            'data'     => $rows,
            'readOnly' => $select->isReadOnly(),
            'rowClass' => $this->getRowClass(),
            'stored'   => true
        );

        $rowsetClass = $this->getRowsetClass();
        if (!class_exists($rowsetClass)) {
            require_once 'Zend/Loader.php';
            Zend_Loader::loadClass($rowsetClass);
        }
        return new $rowsetClass($data);
    }

任何人都知道我做错了什么。

4

1 回答 1

0

我要出去走走,因为我不确定。这感觉可能是报价问题。也许尝试传递您的查询字符串,例如:

//use Zend_Db_Statement to process raw SQL.

//get default adapter
$db = Zend_Db_Table::getDefaultAdapter();
//write SQL statement
$sql = "select * from users where role_id in (3,5) and emailID not in ('abc@abc.com', 'cba@cba.com')";
//assuming MySql as DB instantiate object
$stmt = new Zend_Db_Statement_Mysqli($db, $sql);
//Execute SQL Statement accepts optional parameter for array() of values to be bound
$stmt->execute();

或者您可能想使用 select() 对象构建查询:

//get default adapter
$db = Zend_Db_Table::getDefaultAdapter();
//instantiate the select() object
$select = new Zend_Db_Select($db);
$select->from('users');
//two where will build an AND query use $select->orWhere() to build an OR query 
$select->where('role_id in (3,5)');
$select->where("emailID not in ('abc@abc.com', 'cba@cba.com')");
$result = $model->fetchList($select);

如果$whereString是一个有效的 PHP 字符串,它可能会起作用,因为fetchAll()接受一个字符串或一个实例Zend_Db_Select

$whereString = "role_id in (3,5) and emailID not in ('abc@abc.com', 'cba@cba.com')";

希望这对一些人有所帮助......我开了一枪。

于 2012-04-22T08:58:49.410 回答