Note that there are some explanatory texts on larger screens.

plurals
  1. POMany_Many Relationship Fails with Property "ClassName.RelationName" is not defined
    text
    copied!<p>Basically, I want to build the relation between the tables 'cities' and 'images'. They both have an ID column which is constrained by a third table called cities_images. Here's the structure of the table in the middle:</p> <pre><code>CREATE TABLE IF NOT EXISTS `cities_images` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cityId` int(11) NOT NULL, `imageId` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `imageId` (`imageId`), KEY `cities_images_ibfk_2` (`cityId`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; ALTER TABLE `cities_images` ADD CONSTRAINT `cities_images_ibfk_2` FOREIGN KEY (`cityId`) REFERENCES `cities` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `cities_images_ibfk_1` FOREIGN KEY (`imageId`) REFERENCES `images` (`id`) ON DELETE CASCADE ON UPDATE CASCADE </code></pre> <p>I have two models that work with the cities and images tables.</p> <p>Relation inside Cities.php:</p> <pre><code>'citiesImages' =&gt; array(self::MANY_MANY, 'CitiesImages', 'cities_images(id,cityId)'), </code></pre> <p>Relation inside Images.php:</p> <pre><code>'citiesImages' =&gt; array(self::MANY_MANY, 'CitiesImages', 'cities_images(id,imageId)'), </code></pre> <p>I am trying a test action inside CitiesController with the following content:</p> <pre><code>$cities = Cities::model()-&gt;findByPk(2); var_dump($cities-&gt;images);exit; </code></pre> <p>But this results in an error:</p> <blockquote> <p>Property "Cities.images" is not defined.</p> </blockquote> <p>Note that there is a city with ID=2 and there is a row in cities_images with cityId=2 and imageId=1. There is also a row in the images table with ID=1, so I don't see why I can't access the images associated with the given City.</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