Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7805747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:18:48+00:00 2026-06-02T02:18:48+00:00

Suppose I’m writing a blog app: My Posts Entity should have a createddate ,

  • 0

Suppose I’m writing a blog app: My Posts Entity should have a createddate, modifieddate, name, and slug property (the slug would be generated when I call setName() for the Entity). I want to reuse this code, but I also want to reuse it on-demand. I may, for instance, have a Log Entity which wants to use createddate, but not modifieddate or slug functionality.

Class inheritance, whether via traits (mixins) or abstract classes, seems to be an insufficient, or at least improper, approach to reusing this functionality, as it doesn’t pass the is-a test. After all, the Entity has-a createddate, but isn’t a createddate itself.. so we should use Composition rather than Inheritance, right?. However, observer doesn’t seem to work here, since while I want to use this functionality on-demand, Doctrine’s use of annotation and object properties seem to make horizontal injection difficult (and expensive?) without use of Reflection.

To show some code and simplify the question a bit: can I at once inject the following definition and functionality into an Entity without breaking DRY or good OOP (proper use of composition / inheritance) practice?

/**
 * @var DateTime $createddate
 *
 * @ORM\Column(type="datetime")
 */
private $createddate;

/**
  * @ORM\PrePersist
  */
public function createddatePrePersist() {
    $this->createddate = new \DateTime('now');
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:18:51+00:00Added an answer on June 2, 2026 at 2:18 am

    My personal opinion is that you’re trying to find the nail for your hammer. You don’t have to use composition, inheritance, traits, or any OOP pattern for every entity.

    I tend to consider that the creationDate is an inherent property of my entity, and any entity that requires a creation date has this property. One may argue that’s repeating myself, but in my opinion that makes the whole entity much easier to read.

    Oh, and if I may, I wouldn’t use an ORM-specific method, which is not part of my domain model, to initialise the creation date. This would be done in my constructor:

    /**
     * @var DateTime $createddate
     *
     * @ORM\Column(type="datetime")
     */
    private $createddate;
    
    public function __construct()
    {
        $this->createddate = new \DateTime();
        // ... and other business rules that need to be enforced in the constructor
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have the name written in any none-latin letters - languages, like Arabic,
Suppose I have the following table: PERSON : ID INT NAME STRING LASTNAME STRING
Suppose I have @interface A : NSObject @property (nonatomic, strong) NSMutableArray *array; @end Later
Suppose I have a variable number of static image resources that I would like
Suppose I have the following script, called include_strict.js . After it executes I should
Suppose, I have a Customer class with some properties like name, id, object of
Suppose I decide the ideal domain name for my next project would be foo.com.
suppose i have a .on() function wherein i select multiple ids $(#i, #am, #your,
Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')
Suppose I have a pure virtual method in the base interface that returns to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.