0

I'm using Symfony2 with Doctrine. I have tons of entities (tables) and here's what I'm trying to accomplish:

PROBLEM

Every entity needs to have 2 fields (columns), for example from_date and to_date by default. Suppose I have the following entity defined in the product.orm.yml file:

Some\Random\Namespace\Product:
    type: entity
    table: product
    id:
        id:
            type: integer
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
        description:
            type: text
        price:
            type: float

After I would run doctrine:schema:update the created table should already have from_date and to_date columns. Now, lets say I'm trying to fetch all the products from the database. I would do it like so:

$products = $this->getDoctrine()->getRepository('SomeRandomBundle:Product')->findAll();

This would normally fetch ALL the products from the database. What I would like to do, is to fetch only products where from_date <= some_date <= to_date. There would be a function getSomeDate() that Doctrine would have to call in order, to get the value of some_date. Not that it should also work with custom queries written in DQL or if I do something like this

$products = $this->getDoctrine()->getRepository('SomeRandomBundle:Product')->findByPrice(20);

However, it should not affect cases, where I try to fetch the entity by it's PK using the find method or custom DQL query.

ADDITIONAL DETAILS

There would also be some entities, that would not follow the above rules.

I know I could create from_date and to_date in every entity + perform the check, but since there will be thousands of queries in the application it would only make code less readable.

GOAL

The goal of the above is to allow user, to see the state of the application as it was, anywhere in the past. If you know a different approach that can achieve the same goal it would also be great.

Thank you for your time!

4

1 回答 1

1
  1. 要将自定义字段添加到您的所有实体,您可以

  2. 要在获取数据时进行额外检查,您还可以设置自定义存储库类,其中 findAll/findBy 使用自定义 DQL 和额外检查

  3. 要在执行 DQL 查询时添加其他检查,您可以指定DQL 自定义 walkers

于 2012-11-13T21:30:53.390 回答