0
votes

Is it possible to have simple read-only entities, that can have an association with an other doctrine entity, but their data is stored in a text ( YAML ) file ?

Let's say I have a product entity, and I want to set a category for each product. But for now, I only have very few categories ( and don't need to edit or add ), so I don't want/need to create a full doctrine entity with it's own table in the DB.

So I create a very simple entity:

class ProductCategory
{

    private $id;
    private $name;

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

}

Now I would like to create a simple YAML file where the data is stored, like so:

0: Cheese
1: Meat
2: Dairy Products
....

Then I would like to set a ManyToOne relation from the product entity to the ProductCategory entity.

Is that possible ? And, how to "query" the categories ? ( let's say I want to list all categories that start with a certain letter )

'Why' you ask ? Well for now, as I said, I only have very few categories, but maybe some day I want to add many more, and even have a CRUD editor for them and so on, then I could easily convert it to a full doctrine entity.

Or any other suggestions on how to approach this ?

1

1 Answers

2
votes

There is already a library that provides what you are looking for that's called Alice:

This way you can create random test data en masse and can still work with Doctrine as usual.

If you want to do this manually it will be a pain to solve the problem of connecting the entities. Your best bet is to keep all of them in arrays with id's being used as keys but even then you will probably end up writing lots of glue code to connect the entities.