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 8318543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:00:41+00:00 2026-06-08T22:00:41+00:00

I have several database tables with 2 primary keys, id and date . I

  • 0

I have several database tables with 2 primary keys, id and date. I do not update the records but instead insert a new record with the updated information. This new record has the same id and the date field is NOW(). I will use a product table to explain my question.

I want to be able to request the product details at a specific date. I therefore use the following subquery in DQL, which works fine:

WHERE p.date = (
    SELECT MAX(pp.date)
    FROM Entity\Product pp
    WHERE pp.id = p.id
    AND pp.date < :date
)

This product table has some referenced tables, like category. This category table has the same id and date primary key combination. I want to be able to request the product details and the category details at a specific date. I therefore expanded the DQL as shown above to the following, which also works fine:

JOIN p.category c
WHERE p.date = (
    SELECT MAX(pp.date)
    FROM Entity\Product pp
    WHERE pp.id = p.id
    AND pp.date < :date
)
AND c.date = (
    SELECT MAX(cc.date)
    FROM Entity\ProductCategory cc
    WHERE cc.id = c.id
    AND cc.date < :date
)

However, as you can see, if I have multiple referenced tables I will have to copy the same piece of DQL. I want to somehow add these subqueries to the entities so that every time an entity is called it adds this subquery.

I have thought of adding this in a __construct($date) or some kind of setUp($date) method, but I’m kind of stuck here. Also, would it help to add @Id to Entity\Product::date?

I hope someone can help me. I do not expect a complete solution, one step in a good direction would be very much appreciated.

  • 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-08T22:00:42+00:00Added an answer on June 8, 2026 at 10:00 pm

    I think I’ve found my solution. The trick was (first, to update to Doctrine 2.2 and) using a filter:

    namespace Filter;
    
    use Doctrine\ORM\Mapping\ClassMetaData,
        Doctrine\ORM\Query\Filter\SQLFilter;
    
    class VersionFilter extends SQLFilter {
        public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias) {
            $return = $targetTableAlias . '.date = (
                SELECT MAX(sub.date)
                FROM ' . $targetEntity->table['name'] . ' sub
                WHERE sub.id = ' . $targetTableAlias . '.id
                AND sub.date < ' . $this->getParameter('date') . '
            )';
    
            return $return;
        }
    }
    

    Add the filter to the configuration:

    $configuration->addFilter("version", Filter\VersionFilter");
    

    And enable it in my repository:

    $this->_em->getFilters()->enable("version")->setParameter('date', $date);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several database tables that just contain a single column and very few
I have a database with several hundred tables imported from a third source. Using
I have an Access database (in Access 2003) with several tables, and data must
Let's say we have system A comprising a MySQL database, with several tables. After
I have a SQL Server 2005 database with several tables. One of the tables
I have a SQL Server Database with several tables. One of them has ID
I have a project that necessarily spans several databases. One database has tables: CREATE
I have a small SQL CE 4.0 database with several tables, mapped using Entity
I have several different tables in my database and I'm trying to use Sphinx
In my SQL Server 2008 Enterprise I have a database that contains several tables:

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.