Code Monkey home page Code Monkey logo

Comments (9)

catfan avatar catfan commented on May 19, 2024

http://medoo.in/api/where #Full Text Searching

from medoo.

tecnicaz avatar tecnicaz commented on May 19, 2024

Thank you for your quick reply, but I had already read that section in Documentation, but I still can't accomplish the following ( and perhaps I stated this poorly in the beggining ). which is do a string search that begins with e.g. FOO% or ends with e.g. %BAR. When I take a look at the queryString there is always a % sign on either side of string.

from medoo.

JulioSimon avatar JulioSimon commented on May 19, 2024

A fast and easy solution for your request is to add a new IF statement inside "where_clause" function with a new label for the operation you want to implement, for example:
We want to remove the first '%' when using the LIKE operation ('FOO%') so lets start to make a new LIKE operation named as "xLIKE".

if (isset($where['xLIKE']))
    {
        $like_query = $where['xLIKE'];
        if (is_array($like_query))
        {
            $is_OR = isset($like_query['OR']);

            if ($is_OR || isset($like_query['AND']))
            {
                $connector = $is_OR ? 'OR' : 'AND';
                $like_query = $is_OR ? $like_query['OR'] : $like_query['AND'];
            }
            else
            {
                $connector = 'AND';
            }

            $clause_wrap = array();
            foreach ($like_query as $column => $keyword)
            {
                if (is_array($keyword))
                {
                    foreach ($keyword as $key)
                    {
                        $clause_wrap[] = $this->column_quote($column) . ' LIKE ' . $this->quote('' . $key . '%');
                    }
                }
                else
                {
                    $clause_wrap[] = $this->column_quote($column) . ' LIKE ' . $this->quote('' . $keyword . '%');
                }
            }
            $where_clause .= ($where_clause != '' ? ' AND ' : ' WHERE ') . '(' . implode($clause_wrap, ' ' . $connector . ' ') . ')';
        }
    }

Also, remember to add your new xLIKE option inside the explode list (function where_clause(...)):

$single_condition = array_diff_key($where, array_flip(
    explode(' ', 'AND OR GROUP ORDER HAVING LIMIT LIKE MATCH xLIKE')
));

And the usage of this new option is the same as the LIKE one but naming it as xLIKE:

$data = $conn->select("item_data", [

    "[>]item_general"   =>  ["id" => "id"]

],[

    "item_general.country",
    "item_general.lastupdate",
    "item_data.title",
    "item_data.category",
    "item_data.price",
    "item_data.currency",
    "item_data.url_details"

],[

    "xLIKE" => [

        "item_data.title" => "Title"    

    ],

    "ORDER" => "item_data.price"

]);

Regards.

from medoo.

catfan avatar catfan commented on May 19, 2024

That will be better I think:

$database->select("account", [
    "user_id",
    "user_name",
],[

    "LIKE" => [
        // Match %title%
        "content" => "something"

        // Match %title
        "%content" => "something"

        // Match title%
        "content%" => "something"
    ]

]);

from medoo.

JulioSimon avatar JulioSimon commented on May 19, 2024

Definitely that is a better solution!

Also I suggest to put that example at the Medoo documentation.

http://medoo.in/api/where #Full Text Searching

from medoo.

catfan avatar catfan commented on May 19, 2024

@JulioSimon Yes, until the new version with this feature released.

from medoo.

catfan avatar catfan commented on May 19, 2024

Added, like the sample above.

$database->select("account", [
    "user_id",
    "user_name",
],[

    "LIKE" => [
        // Match %title%
        "content" => "something"

        // Match %title
        "%content" => "something"

        // Match title%
        "content%" => "something"
    ]

]);

from medoo.

tecnicaz avatar tecnicaz commented on May 19, 2024

Thank you, it worked like a charm

from medoo.

pwwiur avatar pwwiur commented on May 19, 2024

if you mean that "text%" does not working you should fix the ~ operator section in data_implode method

fix these lines:

	if ($operator == '~' || $operator == '!~'){
		if ($type != 'array'){
			$value = array($value);
		}
		$like_clauses = array();
		foreach ($value as $item){
			$item = strval($item);
			if (preg_match('/^(?!(%|\[|_])).+(?<!(%|\]|_))$/', $item)){
				$item = '%' . $item . '%';
			}
                        elseif(substr($item, -1) == "_"){
                            $item = $item . '%';
                          }
                           
			$like_clauses[] = $column . ($operator === '!~' ? ' NOT' : '') . ' LIKE ' . $this->fn_quote($key, $item);
		}
		$wheres[] = implode(' OR ', $like_clauses);
	}

from medoo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.