Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP mySQL constructing parent data from child class
    primarykey
    data
    text
    <p>I have a database with 3 tables. <code>person</code>, <code>player</code>, and <code>coach</code>. The <code>person</code> table contains things that the <code>player</code> and <code>coach</code> have in common, such as <code>firstName</code>, <code>lastName</code>, and <code>email</code>. The <code>player</code>, and <code>coach</code> tables then have a link back to the <code>person</code> table with a <code>personId</code> field. Every person who accesses the site has a <code>person</code> entry. Additionally, some users may also have <code>player</code> or <code>coach</code> entries. The database is set up this way to maintain normalization.</p> <p>In my code, I have a <code>person</code>, <code>player</code>, and <code>coach</code> class. The <code>player</code> and <code>coach</code> inherit from <code>person</code>. Below is a truncated version of the <code>person</code> and <code>player</code> classes.</p> <pre><code>class person{ private $firstName; private $lastName; public function __construct($firstName, $lastName){ $this-&gt;firstName = $firstName; $this-&gt;lastName = $lastName; } } class player extends person{ private $position; private $jersey; public function __construct($firstName, $lastName, $position, $jersey){ parent::__construct($firstName, $lastName); $this-&gt;position = $position; $this-&gt;jersey = $jersey; } } </code></pre> <p>As a heads up I'm quite new to OOP so bear with me if there's things I don't know.</p> <p>(side question, what are the above classes considered, Views?)</p> <p>Now in order to populate these I use what I understand to be the Model classes (is that right?).</p> <pre><code>class personModel{ public function getPerson($personId){ $sql = "SELECT * FROM `person` WHERE `personId` = '$personId'"; //skipping some sql stuff in here return new person($sql['firstName'], $sql['lastName']); } } </code></pre> <p>But now the heart of my question, is how do I implement a <code>playerModel</code>?</p> <pre><code>class playerModel{ public function getPlayer($playerId){ //would I do a join SQL here? //or would I call personModel::getPerson() //or do both these options couple the two classes too tightly? } } </code></pre> <p>Would some kind of <code>factory</code> class be another option? If so, how would that be done?</p> <p>I need to be able to construct <code>person</code>, <code>player</code>, and <code>coach</code> objects, because I have users that will fall into all those categories.</p> <p>Any feedback is greatly appreciated, and I'll be checking back often if I need to clarify anything.</p>
    singulars
    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.
    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