Dimitoriosu Deshirasu
Dimitoriosu Deshirasu

Reputation: 39

Zend Db Where in

I have the following table on MSSQL server:

Superheroes: id,name,real_name,power

And as Input I have the following array:

$heroes=["Superman","Batman","Wonder Woman"]

And I want to fetch their real names using Zend Framework 2.3.2. Therefore on My model I have this function:

public function getHeroesRealNames(array $superHeroesNames)
{
  /**
  * @var Zend\Db\Adapter\AdapterInterface
  */
  $db=$this->db;
  $sql=new Sql($db);
  $qb=new Select('dbo.Superheroes');

  $qb->columns(['real_name'])->where('name IN (?)',$superHeroesNames);

  $query_string=$sql->getSqlStringForSqlObject($bq)
  var_dump($query_string);//Debug Output
}

But the var_dump for debugging purposes returns:

SELECT [dbo.Superheroes].[real_name] AS [real_name] FROM [dbo.Superheroes] WHERE name IN ('')"

Instead of the full query. How can I fix that? please keep in note that I DO NOT use var_dump in production.

Upvotes: 2

Views: 100

Answers (1)

Purple Hexagon
Purple Hexagon

Reputation: 3568

I think you need to use the In Predicate as where method requires this but the method will also create the correct predicate for you if given the correct array or strings.

You can find the docs here .. https://framework.zend.com/apidoc/2.1/classes/Zend.Db.Sql.Predicate.In.html

You don't have to use the class directly, you can do ...

Basic example:

$select->from('Superheroes')->where(array(
    'name' =>$superHeroesNames,
));

You can find examples of this on the docs page too https://framework.zend.com/manual/2.2/en/modules/zend.db.sql.html#where-having

Please note the second argument of where method is the combination type, i.e AND or OR

Upvotes: 1

Related Questions