"should i create an object or work with an array?" Code Answer


you won't be able to solve this with your schema, because you are mixing properties from different entities in your post entity [i.e. username].

the idea is that you just have to make your code aware of the relationship between entities, like:

class post extends entity

  protected $id;
  protected $body;
  protected $date;

  // this will be an instance of a thread entity, let's say
  protected $thread;

  // this will be an instance of the user entity
  protected $user;



this way you will be able to do:

$somepost -> getuser() -> getavatar();

but how do you inject the correct user and thread to each post? that's what object relational mappers are for: in you case, all the sql stuff would be handled by the orm, and you'll be just dealing with you object graph, with no worries about building queries on your own; you just have to define such relationships.

a popular php orm is doctrine: here is an example about how it handles relationships declarations.

of course this approach has downsides, but it's up to you investigating and finding out the best tradeoff between flexibility and dependencies.

By mac13k on April 14 2022

Answers related to “should i create an object or work with an array?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.