Git Product home page Git Product logo

Comments (3)

genhoi avatar genhoi commented on August 24, 2024

You can do this with custom repositories

use Cycle\ORM\Select\Repository;

class UserRepository extends Repository
{

    public function withFilterWhere($column, $operator, $value)
    {
        $r = $this;
        if ($value !== null) {

            $r = clone $this;
            $r->select->where($column, $operator, $value);
        }

        return $r;
    }
}

Sample usage

$code = 1;
$name = 'cat';

/** @var UserRepository $repo */
$repo = $this->orm->getRepository(User::class);

$users = $repo
    ->withFilterWhere('code', 'like', $code)
    ->withFilterWhere('name', 'like', $name)
    ->findAll();

https://github.com/cycle/docs/blob/master/basic/repository.md
https://github.com/cycle/docs/blob/master/advanced/chained-repository.md

from orm.

wolfy-j avatar wolfy-j commented on August 24, 2024

Hi @albertborsos

I'm against adding this feature for 3 main reasons:

  1. The method is confusing, should it skip condition when the value is null/false/empty string?
  2. We are saving 2 lines of code but introducing a new concept of optional conditions, this will make code maintenance harder down the road.
  3. As @genhoi it's much easier to add custom filter logic to repositories (which they are intended for).

Thanks for your suggestion, I'm open to change my opinion if more arguments will surface, this ticket will remain open for the discussion for the next few months.

from orm.

albertborsos avatar albertborsos commented on August 24, 2024

Thanks! This solution is good enough for me.

from orm.

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.