Zend 2-TableGateway Where子句 [英] Zend 2 - TableGateway Where Clauses

查看:92
本文介绍了Zend 2-TableGateway Where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与Zend 2接触,并且在表网关中的where子句中遇到了一些问题.

Hi I am trying to get to grips with Zend 2 and I'm having some problems with where clauses in the table gateway.

下面是我的表类:

//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
    }
}

模型如下:

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

我的作品行得通,但我确实应该能够做类似的事情:

What I've got works but I should really be able to do something like:

public function findOs($userAgent)
{

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

}

但是我无法找出正确的方法.

But I can't figure out the correct way to do it.

编辑(12/11/2012 07:53): 我从Sam的答案中尝试了以下方法,但出现了错误:

Edit (12/11/2012 07:53): I tried the following from Sam's answer but got errors:

$spec = function (Where $where) {
    $where->like('operating_system_type', '%' . $this->userAgent . '%');
};


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

但是出现以下错误:

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子句的示例.我可能缺少了一些东西,但我认为这并不明显.

I would also like to add, that I have read the docs and followed the tutorial. No mention of using Zend\Db\Sql in there. I can no examples of using advanced where clauses inside the tableGateway. I'm probably missing something but I don't think it's obvious.

推荐答案

为什么人们忽略了

Why are people ignoring the official documentation? The simples example would be this:

$artistTable = new TableGateway('artist', $adapter);
$rowset = $artistTable->select(array('id' => 2));

但是,可以为select()函数提供类型为Zend\Db\Sql\Where的参数.再次这部分官方文档有很大帮助.使用Where,您可以执行更简洁的代码,例如:

However, you can give the select() function an argument of type Zend\Db\Sql\Where. Again this part of the official documentation helps a lot. With Where you can do cleaner code things like:

$where = new Where();    
$where->like('username', 'ralph%');

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

希望这对您有所帮助.不要忽略文档! ;)

Hope this helps you a bit. Don't ignore the docs! ;)

这篇关于Zend 2-TableGateway Where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆