简体   繁体   中英

How to search for each word in all fields - Doctrine2

I have a table called Friends are its fields are name, phone, address, skype, birthday and notes. There is also a search filed. How to search in all fields? Will something of this kind work for search word "Ana" and is there a better way?

$qb->select('f')
   ->from('Friends', 'f')
   ->where('f.name = :name')
   ->orWhere('f.phone = :phone')->orWhere('f.address = :address')-> ... 
   ->setParameters( array(
     'name' => 'Ana',
     'phone' => 'Ana', ...  ));

And I would also like to ask how to search for each word (something like fulltext search)? I mean if the user writes "Ana Ivan peter@domain.com" to receive all the row, which have in theirs fields (no matter in which field) Ana, Ivan and peter@domain.com.

For example if Ana and Ivan are the names of two of the friends, but the note for a friend, called John says "A friend of Ivan", and Peter is the name of a friend with email peter@domain.com, the user should receive four rows - Ana, Ivan, John and Peter.

I'm not sure about this, but I think this may work

$search_string = '%".$_POST['search']."%';
$qb-> "select 'f' from 'Friends' where f.name LIKE ':search_string' or f.phone  LIKE ':search_string' or ...";

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