Note that there are some explanatory texts on larger screens.

plurals
  1. POShould I use 'Join's in models, or call other models
    primarykey
    data
    text
    <p>I'm in the process of learning OOPHP, and I'm looking for some 'best practice' advice.</p> <p>With a relational db, there's obviously foreign keys in many tables. When I am creating Models for my site I am trying to determine if it is better to do JOIN queries in the model, or have one model call another?</p> <p>Calling other models seems to keep the code more modular. Either way seem to create dependencies, whether it be on another table or anther model.</p> <p>If I go with the 'call other models' approach I seem to run into another problem: infinite loops. Here's the example I'm running into. I have 2 tables, <code>person</code> and <code>school</code>. Each <code>person</code> has a favorite school, represented by a <code>schoolId</code>. Each <code>school</code> has a <code>principal</code>, that is a <code>personId</code>.</p> <p>The <code>person</code> object the row is mapped to accepts a <code>school</code> object in its constructor, but the <code>school</code> object the school row is mapped to accepts a <code>person</code> object in its constructor.</p> <p>From what I've found, something about lazy loading, seems to be the solution, but (I could be wrong) it seems if I do that I can't use PHP's type hinting.</p> <p>(I'm guessing many will suggest an ORM tool like Doctrine to me, and it's something I will definitely look into in the future. I am avoiding it now because of its supposed steep learning curve, and because I feel I understand those tools better later on if I try it myself once)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload