Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC: Best Way to Query Related Tables
    primarykey
    data
    text
    <p>I've been programming procedural code for quite some time, and recently I've been learning MVC. I have tried a couple of frameworks and decided I wanted to write my own from scratch to learn it inside and out. So far I have a working MVC framework (and it works great!) but I'm trying to determine the best way to query related tables.</p> <p>As of now, in my <code>member</code> model, I use the following to get a list of the members and their <code>rank_name</code></p> <pre><code>SELECT m.*, r.`name` AS rank_name FROM `{$this-&gt;table}` AS m LEFT JOIN `ranks` AS r ON r.`id` = m.`rank_id` </code></pre> <p>This works fine, but it does not seem like the way it's supposed to be done in MVC. After working with frameworks like cakephp, I know that <code>member</code> hasOne <code>rank</code> (or is it <code>rank</code> hasMany <code>member</code>?)</p> <p>The only alternative I thought of was to query the <code>members</code> table by itself in the <code>member</code> model, and then call a method in the <code>rank</code> model to get the rank name for each row, like this:</p> <pre><code>// member model SELECT * FROM `{this-&gt;table}` while($row = $result-&gt;fetch_assoc()) { $data []= $row; $data['rank_name'] = $this-&gt;Rank-&gt;get_name($row['rank_id']); } </code></pre> <p>But this can't be very efficient, having to run a separate query for each member. The only other concept I thought of was using MySQL's <code>IN(x, y, z)</code> function to get the rank names and then merging the arrays somehow.</p> <p>What is the best practice for this in MVC?</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.
    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