B
B
bio2016-09-05 08:09:35
PHP
bio, 2016-09-05 08:09:35

How to design a model class to work with a relational table?

Good afternoon!
Class example:

class FirstModel {

    /** @var int */
    private $id;

    /** @var string */
    private $name;

    /** @var string */
    private $created;

    /** @var string */
    private $modified;


    /** @var Type */
    private $type;

    /** @var Settings */
    private $settings;

    /** @var Settings */
    private $order_settings;

    /** @var Currency */
    private $currency;
}

Table example:
`model_table` (
  `id` int(10) unsigned NOT NULL DEFAULT '0',
  `type_id` smallint(5) unsigned NOT NULL DEFAULT '0',
  `currency_id` smallint(5) unsigned NOT NULL,
  `name` varchar(100) NOT NULL DEFAULT '',
  `settings` text NOT NULL,
  `order_fields` text NOT NULL,
  `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
)

The model will have CRUD properties.
The class, when loaded from the database, must be able to create the necessary objects (Type, Settings, Currency, and others), while we want to achieve loose coupling with the classes of these objects.
There will be several such models to work with similar tables. Confuses that initialization of objects (Type, Settings, Currency and others) can differ.
1) How to achieve this?
2) Is it better to create objects in magic methods when they are accessed and when you need to get data in the form of an object or immediately when loading data from the database?
3) How to make the class more universal?
4) Maybe I'm going in the wrong direction?
I don’t want to use ready-made scripts / packages, I need my own bike.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoliy Lyovkin, 2016-09-05
@Alyovkin

Bicycles are bad. I advise you to look at the implementation of the Data Mapper and Active Record patterns.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question