C
C
codemania2017-07-24 09:50:25
PHP
codemania, 2017-07-24 09:50:25

ORM architecture. Passing table name?

The question is not about any specific ORM, but about the architecture in general.
There are options where you need to pass the table name directly in the query, for example:

$countries = $query->orderBy('code')
->where(['country.code' => '123'])
->count();

country.code - country table, code field, everything is clear here
. There are more magical options like:
$customer = new Customer();
$customer->name = 'James';
$customer->email = '[email protected]';
$customer->save();

And where did he save this client xs. Most likely in the Customer table and its name = class name.
But it happens:
class Customer extends ORM{
  protected $dbtable = 'my_clients'; // реальное имя таблицы БД задано на уровне класса
...
}

Or table names in class methods can be declared. With requests, the level of abstraction rises, but it is more difficult for new developers to understand the system and understand where what comes from and where it is entered.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Talalaev, 2017-07-24
@codemania

Usually the model is called with a single number, and the table in the plural with a small letter.
Those in the second case have the Customer model, the customers table. So for example done in eloquent in Laravel. If this does not suit you, then in normal ORMs you can always specify the table name manually, like you showed in the third case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question