0

I am using PHP with Symfony and Doctrine but my question should be independent from any used language or framework.

Suppose you have an entity Product with a One-To-Many relationship to another entity Price. Price has (among others) the properties validFrom and validUntil. Now I want to know the price of a product on a specific day.

As far as I know this can be accomplished in two ways:

  1. Create a custom getter on the Product entity e.g. getPriceOnDate(date). It would cycle through all associated prices until it finds the right one.
  2. Create a repository function that fetches the correct price directly from the database.

Which one of the two approaches are in line with the MVC Best Practices?

  • 2
    Neither. This is out of the scope of the MVC design pattern, and thus there are no best MVC practices for this. – Greg Burghardt Jan 4 '18 at 13:53
  • Thank you for your comment. Can you help me modify the question so that it reflects what I actually want to know? I still don't think, that my question is specific to symfony/doctrine so there has to be a more general common ancestor (but which?) – Emanuel Oster Jan 4 '18 at 14:55
2

As Greg's comment says this doesn't have much to do with MVC, as all MVC says is that this sort of thing would be in the model/repository, rather than mixed in with presentation logic, which would be the case in both of your suggestions.

As for your question, it can depend on exact requirements, but usually doing this sort of thing in the database query will be better for performance than reading each element into the application and filtering them there, as you can take advantage of indexes.

  • 2
    And before deciding to do this with a database query you need to determine whether or not the performance difference is big enough to spend human time writing code that the orm mostly already does. – Greg Burghardt Jan 4 '18 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.