nextras / orm

Orm with clean object design, smart relationship loading and powerful collections.
https://nextras.org/orm
MIT License
309 stars 59 forks source link

Support for LIKE operator #250

Closed dasim closed 7 years ago

dasim commented 7 years ago

Hi, great library, using it for a while successfully, so thanks for your work!

I wondered, will there be (or am I missing already) support for easy conditions with LIKE %% operator? My need is a simple and easy search by name LIKE %something%. I currently use specific method in a mapper that’s made available by annotation in corresponding repository. It works fine.

Here is an example of such simple mapper:

<?php

namespace App\Model;

class UserMapper extends \Nextras\Orm\Mapper\Mapper
{
    protected $tableName = 'user';

    public function filterSearch($params)
    {
        $qb = $this->builder();
        if (isset($params['search_name']) && $params['search_name']) {
            if (!empty($terms = $this->splitString($params['search_name']))) {
                foreach ($terms as $t) {
                    $qb->andWhere($this->tableName . '.name LIKE %_like_', $t);
                }
            }
        }
        return $this->toCollection($qb);
    }

    private function splitString($string)
    {
        $string = $string ? : '';
        return array_filter(array_map(function ($t) {
            return trim($t);
        }, explode(' ', $string)));
    }
}

I’m currently testing v3.0.0 beta and it works just fine. But when I get to more complex conditions using ICollection::OR operator, my mapper gets more complex as well by trying to find the search_name parameter and also I’m not really sure if it will work well with ->andWhere part in my filterSearch method.

It would be great if it would be possible to use something like $this->orm->users->findBy(['name LIKE' => '%something%']) or similar instead. Do you think it’s a feasible to ask for that?

hrach commented 7 years ago

Support for this was implemented through Custom functions, which are not documented yet (a new feature in 3.0), take a look here: https://github.com/nextras/orm/issues/127#issuecomment-324424227

dasim commented 7 years ago

Cool, thanks a lot. I think I was on the right path with Custom functions while exploring v3 but lack of documentation stopped. Great to know it’s coming!

dasim commented 7 years ago

Closing, will be finished by #251.

hrach commented 4 years ago

LIKE support was just merged into 4.0. See PR #416.