Zend 2 - TableGateway Where Clauses

Aydin Hassan picture Aydin Hassan · Nov 11, 2012 · Viewed 13.9k times · Source

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

Below is my table class:

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

The model is like so:

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.

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

But got the following error:

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.

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.

Answer

Sam picture Sam · Nov 12, 2012

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

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

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! ;)