简体   繁体   English

喜欢在zend框架2中的查询

[英]like in where query in zend framework 2

I am using the Zend framework 2.x and facing the problem as I have search a lot. 我使用Zend框架2.x并面对问题,因为我已经搜索了很多。 I want to use the like clause in query but each time gives the errors: 我想在查询中使用like子句,但每次都会给出错误:

Here is my efforts: 这是我的努力:

$sql = new Sql($this->adapter);
$select = $sql->select()->columns(
array('user_profile_id', 'profile_login_name'))->from($this->table)->where->like(
       'profile_login_name', '%'.$strSearch.'%');
echo $select->getSqlString(); die;

but this gives the error: 但是这给出了错误:

Fatal error : Call to undefined method Zend\\Db\\Sql\\Where::getSqlString() in /var/www/YAAB/branches/admin/models/Model/UserTable.php on line 131 致命错误 :在第131行的/var/www/YAAB/branches/admin/models/Model/UserTable.php中调用未定义的方法Zend \\ Db \\ Sql \\ Where :: getSqlString()

I have also used the Zend\\Db\\Sql\\Predicate but this also gives the error. 我也使用了Zend \\ Db \\ Sql \\ Predicate但这也给出了错误。

So my question are that 所以我的问题是

  1. how to use the like clause in query in zend framework 2? 如何在zend框架2中使用查询中的like子句?
  2. What is problem in my code? 我的代码有什么问题?

Please reply soon as it is urgent. 请尽快回复,因为它很紧急。

Try this out 试试吧

$select = $sql->select(); // or new Select('table');
$where = new \Zend\Db\Sql\Where();

// Using predicates
$where->addPredicate(
    new \Zend\Db\Sql\Predicate\Like('my_field', '%test%')
);

// Alternatively, a shortcut
$where->like('my_field', '%test%'); // Alternatively, a shortcut.

$select->where($where);

// this part will depend on if you're using TableGateway or what ever

$stmt = $sql->prepareStatementForSqlObject($select);
$resultSet = new ResultSet();
$resultSet->initialize($stmt->execute());

You can use Predicator to use Like. 您可以使用Predicator来使用Like。

use Zend\Db\Sql\Predicate\Like

$where = new Where();
$where->like('my_field', '%' . $test . '%');

$select->where($where);

Note: And to use Not Like, you can use Literal instead. 注意:要使用Not Like,您可以使用Literal。

$where->literal('my_field NOT LIKE ?', '%' . $test . '%');

I use like that will help me to get much simpler. 我用这样会帮助我变得更简单。

    <?php

    namespace WebApp\Table;

    use Zend\Db\TableGateway\TableGateway;
    use Zend\Db\Sql\Where;
    use Zend\Db\Sql\Sql,
        Zend\Db\Adapter\Adapter;
    use Zend\Db\Sql\Expression;

    class ClassName
    {    

        public function sidebarJobByUser(\WebApp\Entity\User $user)
            {
                $userId  = $user->getId();
                $adapter = $this->tableGateway->getAdapter();
                $sql     = new Sql($adapter);

                $select = $sql->select();
                $select->from($this->table)
                       ->columns(array('user_profile_id', 'profile_login_name'))
                       ->where->like('profile_login_name', '%'.$strSearch.'%');

                $statement = $sql->getSqlStringForSqlObject($select);
                $results   = $adapter->query($statement, $adapter::QUERY_MODE_EXECUTE);

                return $results;
            }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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