Giter Club home page Giter Club logo

housekeeper's People

Contributors

aaronjan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

baisoo ipengxh

housekeeper's Issues

WhereHas in criteria

Hello,

I am trying your package. Seems great !

How would you add a whereHas constraint to the query ? (could be withTrashed())
I tried to create a criteria, but the model property is protected and therefore inaccessible.

E.g :

/**
     * The constructor
     * @param string $relation
     * @param string $attribute
     * @param string $value
     * @param string $operator
     */
    public function __construct($relation, $attribute, $value, $operator = '=')
    {
        $this->relation = $relation;
        $this->attribute = $attribute;
        $this->value = $value;
        $this->operator = $operator;
    }

    /**
     * Apply the criteria
     * @param  RepositoryInterface $repository
     */
    public function apply(RepositoryInterface $repository)
    {
        // Determine whether 'and' or 'or'
        $method = is_null($this->or) ? 'whereHas' : 'orWhereHas';

        // Get the the attributes as an array
        $relations = explode('.', $this->relation);

        // Create closure for the whereHas method
        $closure = function($q) use(&$closure, &$relations)
        {
            if (count($relations) > 0)
            {
                $q->whereHas(array_shift($relations), $closure);
            }
            else
            {
                $q->where($this->attribute, $this->operator, $this->value);
            }
        };

        // Apply the criteria to the model
        $repository->model->{$method}(array_shift($relations), $closure);
    }

EDIT : I need a criteria since i want to be able to call paginate() after applying this criteria. Looking at your code, I can't see how I can do this with findWhere() or findByFields() (they both call get()).

Thanks.

Question about Before and After Flow

Hello,

I am wondering why the Before and After injections are executed before and after the required method (e.g update(...)) and not within the method ?

To be clear, with the way the flow works, you can't modify the model itself right ? Or did I miss something ?

Example :

Assuming I want to create a new record with a relation.

public function create(array $attributes)
    {
        return $this->wrap(function ($attributes) {

            /////// -- The instance is created after the BEFORE injections -- ///////
            /////// -- We can not modify the model instance before save then ? -- ///////
            $model = $this->model->newInstance($attributes); 
            /**
             * @var Model $model
             */
            $model->save();

            return $model;

        }, new Action(__METHOD__, func_get_args(), Action::CREATE));
    }

So if I want to pass the relation as an argument, I can't create a BEFORE flow that automatically associates the relation to the model.

Of course, I could do it after the call to the repository in my controller / command (or whatever) but :

  • It won't respect the DRY principle.
  • This is not the responsibility of the controller / command to create the associations (this is the repository responsibility)
  • The model would be saved twice (in the repository first, then in the controller / command after the association)

Same issue about the modification of the arguments ? Ex : modify an argument before saving the model.

Thanks,

WithTrashed Criteria

Hello,

Found another issue.
Assuming you have a model that can be softDelete with the SoftDeletes trait.

Every queries built with eloquent will have the following statement : where model.deleted_at is null

Now, if we want to get the trashed, we obviously need a criteria (so we don't have to extend the BaseRepository class and change everything around).

But a criteria only allows us to change the query with the "query builder" (not eloquent). And the withTrashed() method is not available for the query builder.

public function apply(RepositoryInterface $repository)
    {
        // Create the closure
        $closure = function($q)
        {
            // Won't work because $q-> gives us the query builder not eloquent
            $q->withTrashed();
        };

        // Apply the criteria to the model
        $repository->applyWhere([$closure]);
    }

You either need to implement the softDeletes feature or gives us access to the model instance in a criteria.
By only allowing us to access the query builder we are not able to perform several actions coupled to eloquent (like softDeletes).

Thanks,

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.