Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to either add an extra table that will hold which Fighters belongs to a Fight, and limit that to 2 records per fight. Or, you have to have two belongsTo associations.</p> <p>With two belongsTo associations the Fight model would look like this.</p> <pre><code>class Fighter extends AppModel { public $hasMany = array( 'FightA' =&gt; array( 'className'=&gt;'Fight', 'foreignKey'=&gt;'fighter_a_id' ), 'FightB' =&gt; array( 'className'=&gt;'Fight', 'foreignKey'=&gt;'fighter_b_id' ) ); } </code></pre> <p>You could then merge the two associated results of FightA and FightB into a single list called Fight. Do this in the controller or maybe the afterFind callback in the model.</p> <p>I think it can be done something like this.</p> <pre><code> $fights = $this-&gt;Fighter-&gt;find(....); $fights['Fighter']['Fight'] = $fights['Fighter']['FightA']+$fights['Fighter']['FightB']; </code></pre> <p>The other approach is to add another table to associate Fighters with a Fight. You could call this table fight_members.</p> <p>It would be a simple table.</p> <pre><code>CREATE TABLE `fight_members` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT , `fighter_id` INT UNSIGNED NOT NULL , `fight_id` INT UNSIGNED NOT NULL , PRIMARY KEY (`id`) ); </code></pre> <p>You can now find all the Fights a Fighter belongs to.</p> <pre><code>class Fighter extends AppModel { public $hasMany = array('FightMember'); } class Fight extends AppModel { public $hasMany = array('FightMember'); ); class FightMember extends AppModel { public $belongsTo=array('Fight','Fighter'); ); </code></pre>
 

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