M
M
Max Ba2019-08-02 13:32:58
PHP
Max Ba, 2019-08-02 13:32:58

How to get multiple objects of the same model?

There is a simple class

class Book
{
  public $id;
  public $name;
    
  public function __construct(int $id, string $name){
    $this->id = $id;
    $this->name = $name;
  }

  //и тд.
}

In conjunction with the collection class, you can get many objects of the same model.
And it is possible, by changing the class, to return a set of objects inside the given one.
What is the best and most convenient way?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
Lander, 2019-08-02
@usdglander

And it is possible, by changing the class, to return a set of objects inside the given one.

That is, the Book will contain many books?

S
Siverius, 2019-08-02
@Siverius

If you make a class a la model, in which to implement the search for a set of objects, and expand it with the Book class, then - quite yes. But, I suspect, that's what you meant by "In conjunction with the collection class".

A
Anton Shamanov, 2019-08-02
@SilenceOfWinter

read about patterns ActiveRecord, DBA, ORM

N
Northern Lights, 2019-08-02
@php666

Definitely the first option.
For searching collections, single objects, and in general for CRUD, it is better to look towards DataMapper - it abstracts model objects from the DBMS. It is very comfortable:
datamapper.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question