1

我根据教程使用 Zf2 组装了一个小型应用程序,我想从数据库中获取数据,但我不得不面对一个奇怪的问题。

我的 TableGateway 类中有这个方法:

public function selectWith($select = null) {

    $filter = new Predicate();
    $filter->like('category_name',"%dess%")
            ->OR
            ->like('category_desc', "%ks%");

    var_dump($this->tableGateway->select(function(Select $select) use ($filter) {
        $select->where($filter);
    }));

    return $this->tableGateway->select(function(Select $select) use ($filter) {
        $select->where($filter);
    });
}

根据转储,执行了以下查询:

SELECT `categories`.* FROM `categories` WHERE (`category_name` LIKE :where1 OR `category_desc` LIKE :where2)

而不是应该由源代码形式化的:

SELECT `categories`.* FROM `categories` WHERE (`category_name` LIKE '%dess%' OR `category_desc` LIKE '%ks%')

这是一个错误,还是我做错了什么?

根据API, Like 只接受两个参数,都是字符串。

我使用采埃孚 2.2.2

提前感谢您的帮助!

4

1 回答 1

0
No, this is not a bug. Please check below how it works:<br />

在查看 select 语句的 vardump 之前

var_dump($this->tableGateway->select(function(Select $select) use ($filter) {
    $select->where($filter);
}));

Please perform the var_dump of $filter. Where you will find the array of like statements.

example:

["predicates":protected]=>
  array(2) {
    [0]=>
    array(2) {
      [0]=>
      string(3) "AND"
      [1]=>
      object(Zend\Db\Sql\Predicate\Like)#377 (3) {
        ["specification":protected]=>
        string(14) "%1$s LIKE %2$s"
        ["identifier":protected]=>
        string(12) "contact_name"
        ["like":protected]=>
        string(4) "%dess%"
      }
    }
    [1]=>
    array(2) {
      [0]=>
      string(2) "OR"
      [1]=>
      object(Zend\Db\Sql\Predicate\Like)#378 (3) {
        ["specification":protected]=>
        string(14) "%1$s LIKE %2$s"
        ["identifier":protected]=>
        string(13) "business_name"
        ["like":protected]=>
        string(4) "%ks%"
      }
    }
  }
}


These two array's ["like":protected] portion are replaced with "where1" and "where2" respectively when the query is being executed. <br />
Thanks :)
于 2013-11-27T06:36:06.063 回答