opulencephp/Opulence

Rewriting own SqlDataMapper classes

Closed this issue · 4 comments

Your solution to use:

  • entity (not found an example)
  • dataMapper
  • repository
  • unit of work
// Assume $dataMapper and $unitOfWork are already instantiated
$repo = new Repository(Post::class, $dataMapper, $unitOfWork);

I cannot see how Post class look like - and this is quite important!

But the clue of this issue is to make some relations between repository to set in one place:

  • table name,
  • table columns,
  • make simple SQL resolver, which will automatically build SQL query.

Examples from documentation are quite simple so you can make new definitions of add(), delete(), update() methods, and if programmer would need to customize them he/she will override them with own methods (e.g. use only some of columns).

I know that writing complex ORM is not so easy, but I would help you to get more programmers using Opulence :)

The Post class is just a plain-old PHP class. You do not have to implement or extend any interface/base class. The issue with trying to have the ORM auto-write queries for you is that it assumes a certain DB schema, which I find to be very limiting to the developer. If your tables/entities are very simple and you'd like that auto-wiring support, I'd recommend an active record ORM.

It would be nice to extend the "Basic Usage" documentation though.

I had similar issues starting out. I figured it out but for a first time developer it would be beneficial to show a Post::class code with a corresponding DB table INSERT example to start them off in the right direction.

Then you could add your reasoning why you chose this way (schema is limiting, extending is hard, etc), flesh out the docs a bit more?

@connerbw Fair point. I've created an issue over in the doc repo to do this.

@evolic I'm going to mark this closed because I've created an issue in the documentation repo to better explain the design choices behind the data mapper.