比如zend framework 2中的where查询

时间:2021-07-22 22:44:49

I am using the Zend framework 2.x and facing the problem as I have search a lot. I want to use the like clause in query but each time gives the errors:

我正在使用Zend framework 2。由于我搜索了很多次,所以我想在查询中使用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

致命错误:调用未定义的方法Zend\Db\Sql\在/var/www/ yaab/branches/admin/models/model/usertable中::getSqlString()。php在第131行

I have also used the Zend\Db\Sql\Predicate but this also gives the error.

我还使用了Zend\Db\Sql\谓词,但这也给出了错误。

So my question are that

我的问题是。

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

Please reply soon as it is urgent.

请尽快回复,因为很急。

3 个解决方案

#1


11  

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());

#2


14  

You can use Predicator to use 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,你可以使用文字。

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

#3


6  

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;
            }
    }

#1


11  

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());

#2


14  

You can use Predicator to use 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,你可以使用文字。

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

#3


6  

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;
            }
    }