Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp: HasOne Relationship
    text
    copied!<p>I'm trying to model the following:</p> <p>A journey has an id, a fromCity and a toCity.</p> <p>My DB looks like this:</p> <p>Table journey: </p> <pre><code>id | fromCity | toCity 1 2 4 2 4 2 </code></pre> <p>Table fromCity:</p> <pre><code>id | name 2 paris 4 london </code></pre> <p>I have models defined for the city and for the journey.</p> <p>In my model file for the journey I want to declare a $hasOne filed in order resolve the fromCity id to the city's name.</p> <p>I tried to follow the CakePHP tutorial on hasOne (<a href="http://book.cakephp.org/view/80/hasOne" rel="nofollow">http://book.cakephp.org/view/80/hasOne</a>) but I can't wrap my head around how to resolve two foreign keys. </p> <p>Can someone please explain to me how a <code>var $hasone = ...</code> has to look for this case?</p> <p>Edit: The models:</p> <pre><code>&lt;?php class City extends AppModel { var $name='City';} ?&gt; &lt;?php class Journey extends AppModel { var $name='Journey'; /*var $hasOne=array(...)*/} ?&gt; </code></pre> <p><strong>Edit 2:</strong></p> <pre><code>var $hasOne = array( 'CityFrom' =&gt; array( 'className' =&gt; 'City', 'conditions' =&gt; 'Journey.fromAirport = CityFrom.id', 'dependent' =&gt; true, 'foreignKey' =&gt; 'id = Journey.fromCity' ), 'CityTo' =&gt; array ( 'className' =&gt; 'City', 'conditions' =&gt; 'Journey.toCity = CityTo.id', 'dependent' =&gt; true, 'foreignKey' =&gt; 'id = Journey.toCity' ) ); </code></pre> <p>Seems to work for the first entry of the journey table. All other's values are null. I think the problem occours from having two columns with the same name in this query.</p>
 

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