简体   繁体   中英

How to do a MATCH AGAINST with tableGateway in Zend Framework 2?

I need to do a [MATCH ( title, description ) AGAINST ( 'text' )] in a ZF2 application using tableGateway like in the ZF2 skeleton application.

I had high hopes when tried with \\Zend\\Db\\Sql\\Where , but couldn't find any way. Any advices or examples on how to do that ?

My first contribution to stackoverflow.

You can do it this way in ZF2 TableGateway:

$rowSet = $this->someTableGateway->select(function (Select $select) {
      $select->columns(array(new \Zend\Db\Sql\Expression("MATCH(column) AGAINST('Query') AS score")))
             ->where("MATCH(column) AGAINST('\"Query\"' in boolean mode)")
             ->order('score DESC');
    });

var_dump($rowSet->toArray());

I also didn't find any way to use MATCH AGAINST, so I guess you can use LIKE instead :

$rowset = $someTable->select(function (Select $select) {
     $select->where->like('CONCAT(title, description)', '%text%');
});

It seems there is no possible way to do MATCH AGAINST with tableGateway. Only solution is by doing it using the "default" way by using \\Zend\\Db\\Adapter\\Adapter -> query($qry)

How about this?

$db->select()->from('your_table')
    ->where('MATCH (`title, description`) AGAINST (?)', $text)

And it also works in boolean mode:

$db->select()->from('your_table')
    ->where('MATCH (`title, description`) AGAINST (? IN BOOLEAN MODE)', $text)

You should use the expression($expression, $parameters) from the Zend\\Db\\Sql\\Where (Predicate/PredicateSet) API.

https://framework.zend.com/manual/2.2/en/modules/zend.db.sql.html#expression-expression-parameter

For exemple :

  $select->where->expression(" MATCH(title, description) AGAINST( ? ))", $text);

这对我有用。

$select->where("MATCH(title, description) AGAINST ('{$text}' IN NATURAL LANGUAGE MODE)");

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