首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zend 2- TableGateway Where子句

Zend 2- TableGateway Where子句
EN

Stack Overflow用户
提问于 2012-11-12 02:42:49
回答 1查看 13.9K关注 0票数 2

您好,我正在尝试使用Zend2,我在表网关中的where子句中遇到了一些问题。

下面是我的表类:

代码语言:javascript
复制
//module\Detectos\src\Detectos\Model\OperatingSystemTable.php
namespace Detectos\Model;

use Zend\Db\TableGateway\TableGateway;

class OperatingSystemsTable
{

    public function findOs($userAgent)
    {

        $resultSet = $this->tableGateway->select();

        foreach ($resultSet as $osRow)
        {
            //check if the OS pattern of this row matches the user agent passed in
            if (preg_match('/' . $osRow->getOperatingSystemPattern() . '/i', $userAgent)) {
                return $osRow; // Operating system was matched so return $oses key
            }
        }
        return 'Unknown'; // Cannot find operating system so return Unknown
    }
}

模型是这样的:

代码语言:javascript
复制
class Detectos
{
    public $id;
    public $operating_system;
    public $operating_system_pattern;

    public function exchangeArray($data)
    {
        $this->id                       = (isset($data['id']))                              ? $data['id']                       : null;
        $this->operating_system         = (isset($data['operating_system  ']))              ? $data['operating_system  ']       : null;
        $this->operating_system_pattern = (isset($data['operating_system_pattern']))        ? $data['operating_system_pattern'] : null;

    }

    public function getOperatingSystemPattern()
    {
        return $this->operating_system_pattern;
    }
}

我得到的东西是有效的,但我真的应该能够做一些事情,比如:

代码语言:javascript
复制
public function findOs($userAgent)
{

    $resultSet = $this->tableGateway->select()->where('operating_system_pattern like %' . $userAgent . '%';

}

但是我想不出正确的方法。

编辑(12/11/2012 07:53):我从Sam的答案中尝试了以下内容,但得到了错误:

代码语言:javascript
复制
$spec = function (Where $where) {
    $where->like('operating_system_type', '%' . $this->userAgent . '%');
};


$resultSet = $this->tableGateway->select($spec);

但得到以下错误:

代码语言:javascript
复制
Catchable fatal error: Argument 1 passed to Detectos\Model\OperatingSystemsTable::Detectos\Model\{closure}() must be an instance of Detectos\Model\Where, instance of Zend\Db\Sql\Select given.

我还想补充说,我已经阅读了文档并遵循了教程。没有提到在其中使用Zend\Db\Sql。我没有在tableGateway中使用高级where子句的示例。我可能遗漏了什么,但我不认为这是显而易见的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-12 14:29:02

为什么人们忽略了official documentation?举个简单的例子:

代码语言:javascript
复制
$artistTable = new TableGateway('artist', $adapter);
$rowset = $artistTable->select(array('id' => 2));

但是,您可以为select()函数提供一个Zend\Db\Sql\Where类型的参数。再说一次,this part of the official documentation帮了很多忙。使用Where,你可以做更干净的代码,比如:

代码语言:javascript
复制
$where = new Where();    
$where->like('username', 'ralph%');

$this->tableGateway->select($where)

希望这能对你有所帮助。不要忽略文档!;)

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13334220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档