# |
Feb 17th 2020, 16:58 |
flashios09 |
```$this->Proposals->find('yourCustomFinderNameHere', ['id' => $proposalId]);``` |
# |
Feb 17th 2020, 16:58 |
neon1024 |
..and they’re composable too which makes them very powerful |
# |
Feb 17th 2020, 16:57 |
chris301 |
oooh - literally finders on the ORM |
# |
Feb 17th 2020, 16:57 |
neon1024 |
https://book.cakephp.org/3/en/orm/retrieving-data-and-resultsets.html#custom-finder-methods |
# |
Feb 17th 2020, 16:57 |
chris301 |
intrigued to know more… custom finder? |
# |
Feb 17th 2020, 16:56 |
flashios09 |
> just copied and pasted all over the place, so we’re just trying to centralise it create a custom finder |
# |
Feb 17th 2020, 16:56 |
admad |
@chris301 @dan874 Use custom finders |
# |
Feb 17th 2020, 16:55 |
chris301 |
i don’t think it was me lol |
# |
Feb 17th 2020, 16:54 |
chris301 |
:,) |
# |
Feb 17th 2020, 16:54 |
neon1024 |
I used to work with a guy called Chris Miller |
# |
Feb 17th 2020, 16:54 |
chris301 |
to give some idea (i work with @dan874) i’m looking at a line now that is using a 6 deep nested association |
# |
Feb 17th 2020, 16:53 |
dan874 |
just copied and pasted all over the place, so we're just trying to centralise it |
# |
Feb 17th 2020, 16:53 |
dan874 |
yeah so this codebase we've inherited has a lot of them with lots of associations |
# |
Feb 17th 2020, 16:52 |
neon1024 |
Perhaps a custom finder could help somewhat if you have a find with associations you use frequently |
# |
Feb 17th 2020, 16:52 |
admad |
> but what if you use that line in 30 different places? Use friendsofcake/crud :) |
# |
Feb 17th 2020, 16:52 |
neon1024 |
It feels like doing more work than what you’re saving |
# |
Feb 17th 2020, 16:52 |
neon1024 |
:thumbsdown: from me, sorry |
# |
Feb 17th 2020, 16:51 |
dan874 |
so is the general consensus something like this not a good idea? |
# |
Feb 17th 2020, 16:51 |
neon1024 |
That’s a PHP issue, not a Cake issue imho |
# |
Feb 17th 2020, 16:51 |
neon1024 |
Set it’s result to a class property |
# |
Feb 17th 2020, 16:51 |
dan874 |
but what if you use that line in 30 different places? |
# |
Feb 17th 2020, 16:49 |
admad |
So much trouble to save 1 line of code: `$this->Foo->get($id)` |
# |
Feb 17th 2020, 16:48 |
dan874 |
as it seems to kill the session in some way |
# |
Feb 17th 2020, 16:48 |
neon1024 |
It also means if you need a bespoke entity, you’d have wasted that loading |
# |
Feb 17th 2020, 16:48 |
dan874 |
i think the large entity is killing it |
# |
Feb 17th 2020, 16:48 |
dan874 |
by that i mean the middleware |
# |
Feb 17th 2020, 16:48 |
dan874 |
@neon1024 thats actually what we've done and i think thats whats causing the issue |
# |
Feb 17th 2020, 16:48 |
neon1024 |
You could always abstract away the loading of the data into a table method if that was what the issue was |
# |
Feb 17th 2020, 16:48 |
neon1024 |
I don’t really see any benefits to the approach either |
# |
Feb 17th 2020, 16:47 |
neon1024 |
You could use a middleware perhaps, but you’d have to set the entity into the request, and with associated data I would worry about setting a large entity and associated data into the request, as the request is passed to so many places |
# |
Feb 17th 2020, 16:46 |
neon1024 |
..but it wouldn’t allow it to be passed into the method |
# |
Feb 17th 2020, 16:46 |
neon1024 |
You could use `beforeFilter()` for this, and set the found entity to a class property in the controller |
# |
Feb 17th 2020, 16:46 |
flashios09 |
for some magic, i’m not sure but i think that you need to extend the controller and play with the invokeAction and the Reflection class |
# |
Feb 17th 2020, 16:43 |
dan874 |
so instead of `int $proposal` the method could be `Proposal $proposal` |
# |
Feb 17th 2020, 16:43 |
dan874 |
some magic to get the entity |
# |
Feb 17th 2020, 16:42 |
flashios09 |
here you want to add typehint or have some magic to get the proposal entity ? |
# |
Feb 17th 2020, 16:41 |
flashios09 |
yes, this is how cake works |
# |
Feb 17th 2020, 16:40 |
dan874 |
but in cake we have `view(int $proposalId)` and then have to load the entity in inside the controller. |
# |
Feb 17th 2020, 16:40 |
dan874 |
ok so i have a route `Proposals/view/1025173/primary` which maps to a `ProposalController/view` method, that method currently has no typehint as the parameter being passed to it is just a integer, what we can do in laravel is basically setup on the route a model binding so it knows when i give it ID `1025173` that it goes to the proposal table and loads that model in so in the `view(Proposal $proposal)` method like that. |
# |
Feb 17th 2020, 16:38 |
flashios09 |
if you can explain more *what do you want to do*, maybe i can help you but with cake approach not Laravel |
# |
Feb 17th 2020, 16:38 |
dan874 |
so when you get to the controller you already have your table/entity ready loaded, if that makes sense? |