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 I have string Name and Image Photo as properties of a class in
Suppose I have the following HTML: <form id=myform> <input type='checkbox' name='foo[]'/> Check 1<br/> <input
Suppose I have the following table: PERSON : ID INT NAME STRING LASTNAME STRING
suppose the following simple models: class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() class
suppose I have {data: {243232: {id: testid,name: test } }} so how to get
Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose we have the following table data: ID parent stage submitted 1 1 1
Suppose I have an IEnumerable such as a List(TValue) and I want to keep
Suppose, in MATLAB, that I have a matrix, A, whose elements are either 0
Suppose I have a list, in which no new nodes are added or deleted.

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.