#How to "optimize" the use of an $attribute which is also computed for scopes in an Eloquent model?

12 messages · Page 1 of 1 (latest)

plush falcon
#

Hi all,

Imagine we need to compute stats about an Eloquent model. Those stats would be stored in the $attributes property of the model and would be computed leveraging real db Eloquent attributes of this model and of its relationships. I highlight the fact that those stats are not stored in db, i.e.: there is NO db column for them (that's why I would use $attributes property, along with a getter/setter method).

Moreover, we want to add several scope Eloquent methods that would fetch the Eloquent models for which those stats are equal / greater than / lower than / etc. than the other models of this same PHP class.

  • One way to do that is:
    • To write again the computation we did in the getter/setters methods for stats we wrote along with the $attributes property, but in SQL, in the scope functions.

- Another way to do that is:

  • I don't know, that's the goal of this topic. Do you know how to avoid to re-write the PHP logic in SQL please? I would want to re-use the code returning the statistics in both scopes functions AND $attributes property getter/setter for stats.

Thank you in advance, best regards.
Jars.

mild jolt
#

Hi Jars, did you at least try to get it working without worrying about writing the most optimised code first? Usually it's better to do that first and then refactor the code (using tests, preferably) to make it more efficient.

hazy cipher
#

Sounds like you're massively overcomplicating things, you're directly modifying "internal" Eloquent model properties, next you'll be back "how to unset attributes automatically, because when I want to save/update my model it tries to store attributes that aren't columns"
It's hard to tell what exactly you're trying to do (lots of words, little detail), but you'd probably be better off just using accessors and a memoization function such as spatie/once. That way you're not fooling around with internal crap

plush falcon
mild jolt
#

That's also optimisation. And you learn this by just doing it. Make it work and try to improve, refactor duplicate code. You'll gain so much more that way, compared to asking others how to write the best code.

plush falcon
# hazy cipher Sounds like you're massively overcomplicating things, you're directly modifying ...

Thank you very much.

I'm not sure that a memoization solution would be useful for my "problem".

I will explain it simplier (I will edit my OP):

  • I want to compute a stats model attribute, which leverages other attributes from this model and other models. I want to put it on my API Resource 👍

  • I also want to add scopes methods (greater than, equal to, etc) for this same stats model attribute. By definition, I'd need to write the SQL queries that will do the computations. But those computations were already done in Bullet N°1. (n PHP, not in SQL)

plush falcon
#

(but I totally agree with you if we wre speaking about something more "global/general" 👍 )

hazy cipher
# plush falcon Thank you very much. I'm not sure that a memoization solution would be useful f...

I'm sorry but this just makes no sense to me, at all. SQL queries on computations that are just php objects..? I was referring to you overwriting internal values to temporary store values, which sounds like what you're doing. That should just be accessors, and you can use memoization on the return value so it's only calculated once (per request). That sounds like what you want, no clue what that has to do with SQL

plush falcon
# hazy cipher I'm sorry but this just makes no sense to me, at all. SQL queries on computation...

how would you handle a stats non-existing-in-db attribute for an Eloquent model?

  • You would define an accessor, eventually along with memoization. It would not do any SQL query, right? Rather, it would leverage the other (existing-in-db) attributes.

Now: What if, in addition to this accessor, you wanted to define scopes?

  • You would define a scope method. By definition, it would be defined with a SQL query, right?

"Problem": this SQL query would look very much to the PHP accessor computations, right? = a kind of "code dupplication" (it isn't strictly speaking, buuuut it looks very much like code dupplication)
My question is: how to avoid this "problem"? I would want to write once the computations (e.g. : either in SQL, or in PHP), and use it both in the accessor and in the scope methods.

#

(I hope my question is clearer than it was)

hazy cipher
#

Maybe try providing some actual examples, because I just don't understand what you're trying to achieve here.