I have a list of users in my database, e.g. with firstname, lastname and company, which are all textfields. Now I have an autocomplete field where one can enter firstname, lastname and company to find a user that has a similar name or company name. I want to accomplish this with RedbeanPHP. Unfortunately the documentation doesn't explain how to implement multiple conditions into their "finder"-functions. Also the "exec"-function isnt explained very well, because there I don't know how to prepare values from a form into the manual statement.
Here is my solution that does find users when either firstname or lastname or company is entered:
if (!empty($_POST['autocomplete'])) {
$userListbyFirstname = R::find( 'user', ' firstname LIKE ?', [ '%' . $_POST['autocomplete'] . '%' ] );
$userListbyLastname = R::find( 'user', ' lastname LIKE ?', [ '%' . $_POST['autocomplete'] . '%' ] );
$userListbyCompany = R::find( 'user', ' company LIKE ?', [ '%' . $_POST['autocomplete'] . '%' ] );
$userRbList = array_merge($userListbyFirstname, $userListbyLastname, $userListbyCompany);
$userList = array();
foreach ($userRbList as $userRb) {
$userList[] = $userRb['firstname'] . ' ' . $userRb['lastname'] . ' ' . (!empty($userRb['company']) ? $userRb['company'] : '');
}
return json_encode($userList);
}
else
{
return json_encode(array());
}
When someone enters "John Williams" it returns an empty array, even if there is an database entry for it. I would know how to do it with plain php, but not how to accomplish, that those multiple fields can be searched together (= multiple conditions) with RedBeanPHP.
redbeanPHPfindfunction does. It takes the sql string and appends it to the generated SQL after theWHEREstatement. Then it prepares the resultingSQLquery string. So: To match on first and last name use the SQL string:' firstname LIKE ? and lastname LIKE ?'as the SQL. Thewhere clausecan be a complex as you wish. Theexecfunction is similar. - Ryan Vincent$userRbList = R::find( 'user', ' (firstname LIKE ?) or (lastname LIKE ?) or (company LIKE ?)', [ '%' . $_POST['autocomplete'] . '%' ] );doesn't work for example like expected - hardking