FriendsOfCake / crud

Production-grade rapid controller development with built in love for API and Search

Home Page:https://crud.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Creating an option to skip the hardcoded ->where([primaryKey => $id]) in FindMethodTrait

Spriz opened this issue · comments

I faced some issues today when trying to use the Crud.View action on a model with a slug, even though I changed the finder with the correct attributes and everything, the FindMethodTrait keeps applying ->where(['Offers.id' => $id]) after my Finder is actually done.

Would it make sense to put this line: https://github.com/FriendsOfCake/crud/blob/master/src/Traits/FindMethodTrait.php#L60 into an if that can be bypassed with some configuration?

I know I could write my own Action and overwrite _findRecord - but it seems like a reasonable use case? What do you guys think?

One can although circumvent things like this:

public function view(string $slug): Response
    {
        /** @var \App\Model\Entity\Offer $offer */
        $offer = $this->Offers->find('BySlug', ['slug' => $slug])->firstOrFail();

        return $this->Crud->execute('view', $offer->id);
    }

I just feel like doing:

$this->Crud->action()->setConfig('findMethod', ['BySlug' => ['slug' => $this->request->getParam('pass.0')]]);

should have been enough :/

I am happy to throw in a PR I just wanted to know your opinion on this case before doing so :)

commented

If you do as below you wouldn't even need a custom finder 🙂

public function view(string $slug): Response
    {
        $this->Offers->setPrimaryKey('slug');

        return $this->Crud->execute();
    }

Thanks @ADmad - This could have sideeffects for other code depending on ->getPrimaryKey() during that request though AFAIK? :D

commented

Did you actually have any issue or you are just hypothsizing?

All that the ViewAction does is fetch a record and set view var. Even if customizing the finder like you propose is possible, why would someone do

public function view(string $slug): Response
{
      $this->Crud->action()->setConfig('findMethod', ['BySlug' => ['slug' => $slug)]]);

        return $this->Crud->execute();
}

when the same can be achieved by doing

public function view(string $slug): Response
{
    $this->set('offer', $this->Offers->find('BySlug', ['slug' => $slug]);
}

@ADmad

It's a hypothesis - we have quite some behaviors that use $this->getPrimaryKey() - and those would not be using slug field rather than the id if I understand the $this->Offers->setPrimaryKey('slug') correctly

Yeah, just doing that for view seems simpler, in our case it is for all the actions in the controller in said plugin/prefix where we want to use the slug over the id :) Maybe our usecase is too uncommon afterall.

TLDR: I would have liked to place $this->Crud->action()->setConfig('findMethod', ['BySlug' => ['slug' => $slug)]]); in initialize action, and use it across any action using the FindMethodTrait :)

EDIT: Or maybe we should not have used the getPrimaryKey() in our Behaviors and rather hardcoded in the id column, but welp 🤷‍♂