Answer the question
In order to leave comments, you need to log in
The principle of building database models for PHP?
There are 3 main patterns for building database models. These are Data Table Gateway, Actrive Record, Data Mapper.
I would like to understand for what purposes each of them is suitable, to endure the pros and cons.
I do not have enough experience to endure them myself, so I ask for help from the habr of the inhabitants.
Answer the question
In order to leave comments, you need to log in
Hello, Vladislav
You have a link in your profile to a very good book by Martin Fowler: martinfowler.com/books.html#eaa
From it about Active Record: martinfowler.com/eaaCatalog/activeRecord.html
Data Table Gateway: martinfowler.com/eaaCatalog/tableDataGateway .html
Data Mapper: martinfowler.com/eaaCatalog/dataMapper.html
Now some thoughts from personal experience.
Binding a domain model to a relational model is difficult due to the following factors:
* Granularity (a domain model often has a lot of detail)
* Inheritance (needs to be expressed in a relational data schema)
* Identity (determined by equality but code and ID in the database)
* Associations (one-to-many in the code is expressed by an array, in the database schema through foreign keys)
*
Data navigation (in the code - links, in the database schema joins)
If the domain is not too complex, then it may be convenient to add Persistence methods to the Domain Model (Active Record) classes. This will make it easier for programmers to write service code.
In the event that the domain model fits comfortably on the relational schema, but we still want to isolate SQL to comply with the principle of personal responsibility, then it is recommended to use the Data Table Gateway
These are pretty general words. If you clarify the question, I will be happy to clarify the answer :)
Thanks for the answer.
After getting to know and using these patterns, I had a lot of questions that I can't find an answer to :)
I would like to know what patterns are used in larger projects and why is this justified?
In what cases is it better to apply one or another pattern?
After using Data Mapper (Doctrine 2), I was able to highlight the
following Pros:
- Really convenient separation of the logic of working with the database
- SQL code generation (CREATE TABLE)
Cons:
- Implementation of complex selections
- Query optimization
- Collection selection
- Everything needs to be done through the entity manager
- "Eats" a lot of memory
AR pluses:
- Bringing work from the database to the level up
- Ease of use
Cons:
- Impossible data hiding
- A lot of responsibility on one class
- Difficulty in inheriting
Data Gateway classes (Zend_Table):
- Moving the logic for working with the table into a separate class and to the top
- Working with collections (Rowset)
- It is convenient to extend models , and everything is really flexible
Cons:
- You need to create a lot of practically empty classes
- The complexity of inheritance
Really, did I understand everything correctly?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question