Search code examples
phpzend-frameworkzend-db

SUBSTR inside a where with Zend DB v1


I am doing a query with Zend DB (v1) but I can't find a way of include a SUBSTR statement on the query because have multiple parameters.

This is what I want to achieve:

    SELECT *
    FROM myTable
    WHERE SUBSTR(myField, myVar1, myVar2) = 'xxx'

I need to use the Zend DB standard format but where does not seems to accept more than one parameter while I need to pass myField, myVar1, myVar2 as PHP variables ($myField, $myVar1, $myVar2). 'xxx' is not a variable here, is a fixed value.

    $sql = $this->select()
        ->from(["t1" => "myTable"]])
        ->where( ... ); // Not sure how to implement this

Does anybody know how to achieve this?


Solution

  • After some try and error I found this workaround that seems to work:

    $myFieldQuoted = $this->_db->quote($myField);
    $myVar1Quoted  = $this->_db->quote($myVar1);
    $myVar2Quoted  = $this->_db->quote($myVar2);
    
    $sql = $this->select()
        ->from(["t1" => "myTable"]])
        ->where("SUBSTR($myFieldQuoted, $myVar1Quoted, $myVar2Quoted) = 'xxx'");