protected static function loadByQuery($query, $fields = null)
    {
        //
        $table = static::getTable();
        $writer = static::getDatabase()->getWriter();
        $alias = static::getClassName();

        //
        $join = null;
        $allFields = $fields ? $fields : static::getDefaultFields();

        // parse SQL select fields
        $selectFields = $writer->selectFields($allFields, $alias, $join);

        //
        $values = [];
        $whereConditions = [];

        //
        if (isset($query['where'])) {
            $whereConditions[] = '('.$query['where'].')';
            unset($query['where']);
        }

        //
        foreach ($query as $field => $value) {
            $token = ':'.$field;
            $whereConditions[] = "{$field} = {$token}";
            $values[$field] = $value;
        }

        //
        $where = implode(' AND ', $whereConditions);

        // prepare SQL query
        $sql = "SELECT {$selectFields} "
             ."FROM {$table} AS {$alias} {$join} "
             ."WHERE {$where} "
             .'LIMIT 1';

        // fetch data on database and return it
        $result = static::fetch(
            $sql,
            $values,
            true,
            is_string($fields),
            is_null($fields)
        );

        return $result;
    }