src/Entity/Repository.php
/**
     * Builds the query for the findBy operation and returns a MySQLi prepared statement ready to be executed
     *
     * This method is a little bit messy - with more time I would have broken it down into a few smaller methods
     *
     * @param array $select_fields
     * @param $from_table
     * @param array $search_fields
     * @param $search_string
     * @param int $limit
     * @param int $offset
     * @param bool $wild_search
     * @return \mysqli_stmt
     */
Builds the query for the findBy operation and returns a MySQLi prepared statement ready to be executed
This method is a little bit messy - with more time I would have broken it down into a few smaller methods


Builds the query for the findBy operation and returns a MySQLi prepared statement ready to be executed This method is a little bit messy - with more time I would have broken it down into a few smaller methods
private function prepareFindByQuery(
        array $select_fields,
        $from_table,
        array $search_fields,
        $search_string,
        $limit=0,
        $offset=0,
        $wild_search = false)
    {
        $query = "SELECT ";
        if (count($select_fields)) {
            $fields = implode(',', $select_fields);
            $query .= "$fields ";
        } else {
            $query .= "COUNT(*) ";
        }
        $query .= "FROM `$from_table` WHERE ";
        $idx = 0;
        $params = [];
        foreach ($search_fields as $search_field) {
            $query .= ($idx++ > 0 ? " OR " : "") . "$search_field LIKE ?";
            $params[] = "{$search_string}" . ($wild_search ? "%" : "");
        }
        if ($limit > 0) {
            $query .= " LIMIT " . (int)$limit;
        }
        if ($offset > 0) {
            $query .= " OFFSET " . (int)$offset;
        }
        $stmt = $this->mysqli->prepare($query);
        $params = array_merge(array(str_repeat('s', count($params))), $this->refValues($params));
        $ref = new ReflectionClass('mysqli_stmt');
        $method = $ref->getMethod("bind_param");
        $method->invokeArgs($stmt, $params);
        return $stmt;
    }
private function prepareFindByQuery ( array $ select_fields , $ from_table , array $ search_fields , $ search_string , $ limit = 0 , $ offset = 0 , $ wild_search = false ) { $ query = " SELECT  " ; if ( count ( $ select_fields ) ) { $ fields = implode ( ',' , $ select_fields ) ; $ query .= " $ fields " ; } else { $ query .= " COUNT(*)  " ; } $ query .= " FROM ` $ from_table ` WHERE  " ; $ idx = 0 ; $ params = [ ] ; foreach ( $ search_fields as $ search_field ) { $ query .= ( $ idx ++ > 0 ? "  OR  " : " " ) . " $ search_field  LIKE ? " ; $ params [ ] = " { $ search_string } " . ( $ wild_search ? " % " : " " ) ; } if ( $ limit > 0 ) { $ query .= "  LIMIT  " . ( int ) $ limit ; } if ( $ offset > 0 ) { $ query .= "  OFFSET  " . ( int ) $ offset ; } $ stmt = $ this -> mysqli -> prepare ( $ query ) ; $ params = array_merge ( array ( str_repeat ( 's' , count ( $ params ) ) ) , $ this -> refValues ( $ params ) ) ; $ ref = new ReflectionClass ( 'mysqli_stmt' ) ; $ method = $ ref -> getMethod ( " bind_param " ) ; $ method -> invokeArgs ( $ stmt , $ params ) ; return $ stmt ; }
Builds the query for the findBy operation and returns a MySQLi prepared statement ready to be executed
 This method is a little bit messy - with more time I would have broken it down into a few smaller methods
Builds the query for the findBy operation and returns a MySQLi prepared statement ready to be executed This method is a little bit messy - with more time I would have broken it down into a few smaller methods
// Build the query // If we have fields defined, this will be a select query, otherwise it is a count query // Build the rest of the query // Add limit and offset if they have been specified // Using Reflection to handle the fact we have a variable amount of search_fields, thus number of params