relations

i try to setup a relation between two tables. but the result is not correct

he is joining the cities.id with the countries.id inseatd of cities_id_country with countries.id

when i switch de forgeinskey in de relations i get an error.

COUNTRIES

id <PK>

code

country

CITIES

id <PK>

id_country <FK1>

city

COUNRTY MODEL

'city' => array(self::HAS_MANY, 'City', 'id_country'),

CITY MODEL

'country' => array(self::HAS_ONE, 'Country', 'id'),

CONTROLLER

//query


$rows = City::model()->with('country')->findAll($criteria);

VIEW

<?php foreach($cities as $city): ?>


<?php echo $city->city; ?>


<?php echo $city->country->country; ?>


<?php endif; ?>

RESULT



#  	City  	        Country


3 	Amsterdam	Duitsland


14 	Berlijn


13 	Brussel


16 	Groenlo


9 	Groningen


17 	Haarlo


1 	Londen	Nederland


2 	Neede	België


City BELONGS_TO country, not HAS_ONE country.  HAS_ONE is a one-to-one relationship. BELONGS_TO is a many to one relationship.

'country' => array(self::BELONGS_TO, 'Country', 'id_country'),

ooh I see. got it

i removed the relation in the country model.

and changed the realtion in city model to

'country' => array(self::BELONGS_TO, 'Country', 'id_country'),

is there a way to show the queries that were executed after the page is loaded?

Are you sure the relational keys are not corrupted in the table?  Because some of them seem to be working…

awesome.