M
M
Max Ba2019-04-11 16:16:26
PHP
Max Ba, 2019-04-11 16:16:26

Should the model be separated from data inference and lookup?

There is a client model (class Customer)
Management methods: create(), update(), delete()
Data output methods: getAll(), getSome(array()), getOne(id) (output of all records, some, one. By ID).
There is also a search for getByName, getByRegion and other methods.
Question 1 . basis. Whether to create a CustomerSearch class. You can block anything in it, returning only an ID array, which is then passed to the main Customer class. Or is it better to "infinitely populate" the Customer class?
Question 2. There was a desire to pull the create (), update (), delete () methods into the CustomerEdit or CustomerAdmin class. is it worth doing that?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dmitriy, 2019-04-11
@dmitriylanets

1. Yes, it's like a repository

$searchCriteria = new CustomerSearchCriteria;
$searchCriteria->setByName("москва");
$customers = $customerRepository->findByCriteria($searchCriteria);//->CustomerCollection

2. Methods refer to repository methods: $customerRepository->save($customer);//save is create and update in one bottle

C
Cortess, 2019-04-11
@Cortess

Perhaps it makes sense to create controllers that are responsible for searching and outputting data (If this will increase the readability and understandability of the code). As for me, it's better not to make many models if they work with the same entity

T
ThunderCat, 2019-04-11
@ThunderCat

IMHO good style would be to use collections for multiple selection. Something like

$customers = new Collection(new Customer());
$customers->createCollectionByFields(array('somefield'=>'somevalue'));
$customersArray = $customers->getArray(); //return array of objects

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question