# |
Feb 17th 2020, 17:54 |
slackebot |
'action' => $request->getParam('action'), 'prefix' => $request->getParam('prefix') ?: '', 'plugin' => $request->getParam('plugin'), ]); } /** * @var callable $callable */ $callable = [$this, $request->getParam('action')]; $id = $request->getParam('id'); $pass = $request->getParam('pass'); if ($id) { $Table = |
# |
Feb 17th 2020, 17:54 |
slackebot |
TableRegistry::get($request->getParam('controller')); // you can replace the `get` method with a custom finder or a config option // you can merge the pass or overwrite it, this is just an example $pass[0] = $Table->get($id); } // $result = $callable(...array_values($request->getParam('pass'))); $result = $callable(...array_values($pass)); if ($result instanceof Response) { |
# |
Feb 17th 2020, 17:54 |
slackebot |
* @throws \ReflectionException */ public function invokeAction() { debug("invokeAction"); $request = $this->request; if (!$request) { throw new LogicException('No Request object configured. Cannot invoke action'); } if (!$this->isAction($request->getParam('action'))) { throw new MissingActionException([ 'controller' => $this->name . 'Controller', |
# |
Feb 17th 2020, 17:54 |
flashios09 |
```<?php namespace App\Controller; use App\Controller\AppController; use App\Model\Entity\Roomtype; use Cake\Controller\Exception\MissingActionException; use Cake\ORM\TableRegistry; use LogicException; /** * Roomtypes controller */ class RoomtypesController extends AppController { /** * Dispatches the controller action. Checks that the action * exists and isn't private. * * @return mixed The resulting response. |
# |
Feb 17th 2020, 17:54 |
flashios09 |
@chris301 @dan874 this is how you can have an entity with typehint as a passed param in your action, all you have to do is to play a little bit with the `Controller::invokeAction()` method |
# |
Feb 17th 2020, 17:12 |
neon1024 |
As a Cake controller would get the repository class instance automagically 8) |
# |
Feb 17th 2020, 17:11 |
neon1024 |
Perhaps the grey area is the separation between the repository instance and the entity |
# |
Feb 17th 2020, 17:11 |
neon1024 |
> application providing the model based on what it’s asked for |
# |
Feb 17th 2020, 17:07 |
admad |
what you want could be achieved relatively easily in Cake 4 by overriding Controller::getAction() and Controller::invokeAction() |
# |
Feb 17th 2020, 17:05 |
chris301 |
anyway, i’m clocking off for the day - thanks for your help guys - looks like what we’ve wanted to do isn’t possible |
# |
Feb 17th 2020, 17:05 |
chris301 |
to do what we have seen elsewhere, it wouldn’t be the router doing it - rather the application providing the model based on what it’s asked for. it’s interesting to see the differences between frameworks |
# |
Feb 17th 2020, 17:03 |
admad |
making the router pull out entities is muddling of concerns IMO |
# |
Feb 17th 2020, 17:01 |
chris301 |
we inherited this codebase, along with its problems, and have worked to make it a useable base - we are also behind on versions, due to other complications we’ve had along the way. it’s in the long term plan to update, but that hasn’t happened yet |
# |
Feb 17th 2020, 17:00 |
chris301 |
what we’re trying to do is separate out the concerns fully. as for replicating laravel with cake, theres things we’ve got in place to make our jobs easier but we know replicating laravel is nigh on impossible in cake |
# |
Feb 17th 2020, 16:58 |
chris301 |
so we have many of those already in place |
# |
Feb 17th 2020, 16:58 |
neon1024 |
`$this->Posts->find('newest')->find('byAuthor', ['author' => 'Dave'])->find('published');` |
# |
Feb 17th 2020, 16:58 |
admad |
stop trying to replicate laravel with cake :) just state your problem and ask how to deal with it in cake |
# |
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 |