# |
Sep 2nd 2019, 09:45 |
slackebot2 |
agree it's not as elegant as building on top of existing virtual fields functionality, I won't have it run everywhere even if I don't need it, unless I explicitly need it. Is there any way to control where shouldn't the virtual fields be calculated? Or should I keep some complex calculation (if any) still in my custom methods, and move only basic stuff to virtual fields (like full name concatenation from the cookbook)? |
# |
Sep 2nd 2019, 09:45 |
mehov |
https://cakesf.slack.com/archives/C053DPNGT/p1567414394455200 @dereuromark thanks for the tip, I was about to convert the entity to use these, but got thinking. If I define a bunch of `_getSomeCalculation()` methods on my entity, are those going to be called on every initialisation of that entity from now on? Regardless of whether I need that calculation in a given controller/model If I have my custom `calculateSomething()` method, while I |
# |
Sep 2nd 2019, 09:38 |
alexdd55976 |
only select on fields that are needed is a very good advice. |
# |
Sep 2nd 2019, 09:37 |
challgren |
Unless you need entities |
# |
Sep 2nd 2019, 09:36 |
challgren |
Also maybe disableHydration() |
# |
Sep 2nd 2019, 09:36 |
slackebot2 |
cake would just render all the fields from all the tables involved into the query and it got huge because of that. your case may be different, just saying |
# |
Sep 2nd 2019, 09:36 |
mehov |
@turkles there's a good piece of advice right above, make sure you get only the data you need. do not contain what you don't need. sometimes looking at an actual SQL in the debug panel helps (if you're in console, reuse the model method that retrieves data in some controller just to see the SQL) also, if I remember it right, I think I had a slow query which I was able to speed up a bit by explicitly defining which fields to select; otherwise |
# |
Sep 2nd 2019, 09:30 |
turkles |
so at the moment I have a query limit 100, and then do ->each(), then saveMany. If I wrap that in a loop (and iterate query), it should behave? I must have a leak somewhere O_o |
# |
Sep 2nd 2019, 09:27 |
alexdd55976 |
how can i tell authentication plugin to redirect to `/user/login` if user is not authenticated |
# |
Sep 2nd 2019, 09:25 |
challgren |
@turkles also contain and/or retrieve only the fields you need |
# |
Sep 2nd 2019, 09:19 |
dereuromark |
read in chunks/parts, unset() what you dont need in those loops. |
# |
Sep 2nd 2019, 09:15 |
turkles |
I need to go through 2m records, and I keep running out of space, can I clean out memory somehow in my shell? |
# |
Sep 2nd 2019, 09:15 |
conehead |
That is all I tried to say :P |
# |
Sep 2nd 2019, 09:13 |
conehead |
``` public function getDuration(\DateTimeImmutable $now) { $created = $this->getDateTimeCreated(); if ($created > $now) { // the session hasn't started yet return null; } $ended = $this->getDateTimeEnded(); return $created->diff($ended?$ended:$now); } ``` |
# |
Sep 2nd 2019, 09:12 |
conehead |
@mehov well you could do it without a wrapper class at all |
# |
Sep 2nd 2019, 09:12 |
blancessanchez30 |
@dereuromark thank you, I'll change my approach to that. :thumbsup: |
# |
Sep 2nd 2019, 09:11 |
dereuromark |
@blancessanchez30 Dont think of controllers then, but of URL string subset (e.g. /foo/bar/....) |
# |
Sep 2nd 2019, 09:10 |
conehead |
Potentially it will probably cause an (unrecognizable) overhead, but will imho increase readability and seperation a lot |
# |
Sep 2nd 2019, 09:08 |
challgren |
@mehov excluding the entity and doing more `$wrapperClass->calculateWhatever($entity->created);` is more portable |
# |
Sep 2nd 2019, 09:06 |
mehov |
Actually that's more or less what I thought. Say there's an Entity, and the method needs to be use with it only and nowhere else. Then I'm choosing between `$entity->calculateWhatever()` on one hand and `$wrapperClass = new \Some\Class(); $result = $wrapperClass->calculateWhatever($entity);` Having this in the entity seemed more lightweight to me I may have gotten it wrong though |
# |
Sep 2nd 2019, 09:06 |
blancessanchez30 |
@dereuromark I want to set a specific `max_input_vars` on a certain controller. I only need this in one controller, so applying it to all might be too much.. :thinking_face: |
# |
Sep 2nd 2019, 09:04 |
dereuromark |
@blancessanchez30 Not really, why you need this? This is too early to know about specific routing patterns yet. |
# |
Sep 2nd 2019, 09:03 |
challgren |
And speaking to that wouldn’t instantiation of another class cause more overhead potentially, yes its a very small class but theres already an instance of the Entity |
# |
Sep 2nd 2019, 09:03 |
blancessanchez30 |
^ CakePHP 3 |
# |
Sep 2nd 2019, 09:03 |
blancessanchez30 |
Good afternoon. How do you check in htaccess file if your in a specific controller? Is this possible? |
# |
Sep 2nd 2019, 09:00 |
conehead |
I wouldn't say more. But I think it is a matter of responsibility |
# |
Sep 2nd 2019, 09:00 |
dereuromark |
Entities already have a very high cyclic complexity. It only increases with these things. And often those are only needed in a small subset of places. It is a certain architectural choice one has to make sometimes. Where to put things. |
# |
Sep 2nd 2019, 08:58 |
challgren |
If not more |
# |
Sep 2nd 2019, 08:58 |
challgren |
@conehead wouldn’t making a business class take just the same effort for testing? |
# |
Sep 2nd 2019, 08:56 |
conehead |
No if the logic is in the entity. Whenever you create a new DateTime (or similar) you would have to extract that to an additional method to mock it |
# |
Sep 2nd 2019, 08:55 |
mehov |
@conehead harder to test which classes, the business class ones? |
# |
Sep 2nd 2019, 08:54 |
conehead |
Additionally it will be "harder" to test those classes |
# |
Sep 2nd 2019, 08:54 |
challgren |
I put that stuff in `src/Utils` |
# |
Sep 2nd 2019, 08:54 |
mehov |
Got it, where should I put that class then? |
# |
Sep 2nd 2019, 08:53 |
dereuromark |
Good point: You have to be careful to not overbloat the entities. If it becomes too much logic/calculation, then having a business class as wrapper is maybe the better choice. |
# |
Sep 2nd 2019, 08:53 |
conehead |
imho it would not be nice to put such logic into the entities. I would prefer a method that calculates the diff from the created date and then you would just pass a new DateTime to it |
# |
Sep 2nd 2019, 08:53 |
dereuromark |
Also see https://www.dereuromark.de/2019/02/05/virtual-entity-fields-in-cakephp/ :slightly_smiling_face: |
# |
Sep 2nd 2019, 08:52 |
dereuromark |
read about virtual fields :slightly_smiling_face: in docs. |
# |
Sep 2nd 2019, 08:52 |
slackebot2 |
return ($duration->days*24*60)+$duration->i; } ``` |
# |
Sep 2nd 2019, 08:52 |
slackebot2 |
$this->getDateTimeCreated(); $now = new \DateTimeImmutable('now'); if ($created > $now) { // the session hasn't started yet return null; } $ended = $this->getDateTimeEnded(); return $created->diff($ended?$ended:$now); } public function getDurationMinutes() { $duration = $this->getDuration(); if (empty($duration)) { return 0; } |
# |
Sep 2nd 2019, 08:52 |
mehov |
Okay sorry I used `created` as an example, in my case it's ``` public function getDateTimeCreated() { return new \DateTimeImmutable($this->created->format('r')); } public function getDateTimeEnded() { if (empty($this->ended)) { return null; } return new \DateTimeImmutable($this->ended->format('r')); } public function getDuration() { $created = |