简体   繁体   中英

ZF2 mysql functions in select using TableGateway

I'm using ZF2 TableGateway and want to do a select such as where md5(email) = $hash

I've tried to do this in the select with ->select(array('md5(email)' => $hash)); but this doesn't work, I get column not found.

Any suggestions?

Thanks!

If you are using Zend\\Db\\TableGateway.

                  use Zend\Db\Adapter\Adapter;
                  use Zend\Db\Sql\Sql;
                  use Zend\Debug\Debug;

$driver =  array('db' => array('driver' => 'Pdo', 'dsn'=>'mysql:dbname=zf2;host=localhost;
             charset=utf8', 'user'=>'root','pass'=>'pass')
        );
$db = new Adapter($driver);
                  $sql = new Sql($db);
                  $select = $sql->select();
                  $select->from('YourTable');

                  $select->where(array('md5(email)' => $hash));
        OR

                  $select->where->equalTo('md5(email)', $hash);

that may work. untested.

using table gateway is much easier if your query involve single table. from your query below is the solution

->select(array('md5(email) = ?' => $hash));

would work, this will return resultset object to get the result use

$resultset->current(); //to get the current row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM