Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 671
gawpertron Avatar asked Sep 30 '22 05:09

gawpertron


1 Answers

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.

like image 92
dixromos98 Avatar answered Oct 25 '22 16:10

dixromos98