您好,我正在尝试使用Zend2,我在表网关中的where子句中遇到了一些问题。
下面是我的表类:
//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;
}
}我得到的东西是有效的,但我真的应该能够做一些事情,比如:
public function findOs($userAgent)
{
$resultSet = $this->tableGateway->select()->where('operating_system_pattern like %' . $userAgent . '%';
}但是我想不出正确的方法。
编辑(12/11/2012 07:53):我从Sam的答案中尝试了以下内容,但得到了错误:
$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子句的示例。我可能遗漏了什么,但我不认为这是显而易见的。
发布于 2012-11-12 14:29:02
为什么人们忽略了official documentation?举个简单的例子:
$artistTable = new TableGateway('artist', $adapter);
$rowset = $artistTable->select(array('id' => 2));但是,您可以为select()函数提供一个Zend\Db\Sql\Where类型的参数。再说一次,this part of the official documentation帮了很多忙。使用Where,你可以做更干净的代码,比如:
$where = new Where();
$where->like('username', 'ralph%');
$this->tableGateway->select($where)希望这能对你有所帮助。不要忽略文档!;)
https://stackoverflow.com/questions/13334220
复制相似问题