3

My application is growing in complexity, and I'm finding that going from ORM entities directly to the view is insufficient.

I want to add models. They will take the data from my ORM entities, but also add some functionality. For example, my User ORM entity has an endDate property and a trial property. I'd like to add an isExpired method that ensures

  1. The endDate is later than today, and
  2. trial is false

(This is a simple example. My requirements are much more complex, which is why I want to move the logic outside of the view.)

What is the correct way to do this? Say I make a UserModel class. Will the User object be a property on that class, in addition to my other properties/methods? Will I be able to preserve Doctrines ability to fetch all objects in a single query?

1 Answer 1

1

Why don't you just add the isExpired() method directly to the entity? No one forces you to keep just the DB stuff in the entities.

Sign up to request clarification or add additional context in comments.

5 Comments

This won't mess up persistence, or anything like that?
There's also another wrinkle. I'm using Native SQL in this particular case, because I want to fetch related data for each object. So, there's already a 'wrapper class' (I think this is the only way to do it?). Could I bring the new props/methods from my entity along as well, to the wrapper class?
Elnur's suggestion won't mess up persistence at all, you're just processing the entity's data to form to generate a information function. Should work with your wrapper as well, give parent calls or whatnot. I use this technique quite a bit.
No, it won't mess up anything. You can write any method you want. All the ORM cares about is fields with its annotations on it.
And you don't need to use native SQL to fetch related data for each object. Fetch JOINs do that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.