Level-2 / Maphper

Maphper - A php ORM using the Data Mapper pattern
BSD 2-Clause "Simplified" License
52 stars 7 forks source link

Is it possible to write an FIND_OR with same-type filters? #62

Closed rodrigozietlow closed 5 years ago

rodrigozietlow commented 5 years ago

I'm trying to filter by both name and description of a query on a search function, but I have not found the way to do it yet. What I tried:

filter([
    \Maphper\Maphper::FIND_OR => [
        \Maphper\Maphper::FIND_LIKE => [
        "description" => "%$nomeProj%",
        "name" => "%$nomeProj%"
    ]
    ]
]

But doing so results in SELECT * FROM projeto WHERE (((description LIKE :description AND name LIKE :name)))

And doing the opposite, wrapping FIND_OR inside a FIND_LIKE, the query just freaks out SELECT * FROM projeto WHERE (((description :description OR name :name))).

Am I missing something or it isn't possible?

TRPB commented 5 years ago

Apologies for the late reply, the problem here is that you have nothing to OR. FIND_OR will OR together everything inside it.

The following should work, although it's a bit more verbose:

filter([
    \Maphper\Maphper::FIND_OR => [
        \Maphper\Maphper::FIND_LIKE => [
            "description" => "%$nomeProj%"
        ],
        \Maphper\Maphper::FIND_LIKE => [
            "name" => "%$nomeProj%"
        ]
    ]
])
solleer commented 5 years ago

You have to wrap the FIND_LIKE's in an array also to avoid duplicate keys

solleer commented 5 years ago
filter([
    \Maphper\Maphper::FIND_OR => [

[\Maphper\Maphper::FIND_LIKE => [
   "description" => "%$nomeProj%"
  ]] ,
  [\Maphper\Maphper::FIND_LIKE => [
   "name" => "%$nomeProj%"
  ]] 
    ]
])
rodrigozietlow commented 5 years ago
filter([
    \Maphper\Maphper::FIND_OR => [
        \Maphper\Maphper::FIND_LIKE => [
          "description" => "%$nomeProj%"
      ],
      \Maphper\Maphper::FIND_LIKE => [
          "name" => "%$nomeProj%"
      ]
    ]
])

This worked, thank you.

garrettw commented 5 years ago

How did you manage to avoid the issue of duplicate keys with that code?