Search code examples
phpsqlzend-framework2named-parameters

ZF2 Sql using named parameters


Is it possible to use named parameters in a Select, Update or Delete query object in Zend Framework 2? e.g.

$myValue = 'FooBar';

$sql = new Zend\Db\Sql\Sql($adapter);
$select = $sql->select('my_table')
              ->where('my_column = :my_value')
              ->setParameter('my_value', $myValue);

Solution

  • Never done this but found the answer on an older version of ZEND here

    So to answer your question yes it is possible. As it is explained on the website provided. "If you use named parameters, or those that are indicated by a string identifier preceded by a colon character (':'), pass the bind values in an associative array. The keys of this array should match the parameter names. "

    $select = $sql->select('my_table')
                          ->where('my_coumn = :my_value');
                          //->setParameter('my_value', $myValue);
    
            $statement = $sql->prepareStatementForSqlObject($select);
            $result = $statement->execute((array(':my_value' => 'FooBar')));//you pass named parameters here in an associative array
            $resultSet = new ResultSet();
            $resultSet->initialize($result);
    
            return $resultSet->toArray();
    

    Good Luck Mate.